Skip to content

Instantly share code, notes, and snippets.

@Nathan-Nesbitt
Nathan-Nesbitt / ArithmeticOperators.js
Created May 7, 2020 18:44
List of Basic Operators
var a = 1;
var b = 2;
// Addition Operator
console.log(a + b);
// Subtraction Operator //
console.log(a - b);
// Multiplication Operator //
@Nathan-Nesbitt
Nathan-Nesbitt / IfElse.js
Created May 9, 2020 20:56
If/Else Statements
const age = 21;
// If someone is less than 16
if(age < 16) {
console.log("You cannot drink or drive");
}
// If someone is less than 19 but greater than 16
else if(age < 19) {
console.log("You cannot drink but you can drive");
}
const age = 21;
switch(age) {
case 0:
console.log("You cannot drink or drive");
break;
case 1:
console.log("You cannot drink or drive");
break;
case 2:
@Nathan-Nesbitt
Nathan-Nesbitt / BetterSwitchStatement.js
Created May 9, 2020 21:35
Better Switch Statement Example
var fruit = "Apple";
switch(fruit) {
case "Banana":
console.log("Loads of K");
break;
case "Orange":
console.log("Zesty");
break;
case "Apple":
@Nathan-Nesbitt
Nathan-Nesbitt / WhileLoops.js
Created May 10, 2020 00:04
While Loop Example
/*
This prints out the numbers from 0 to 9
*/
// Initialize
var i = 0;
// Evaluate
while(i < 10) {
console.log(i);
/*
Prints out the values from 0 to 9
*/
// Initialization, Evaluation, and Increment
// all in one line
for(var i = 0; i < 10; i++) {
console.log(i);
}
@Nathan-Nesbitt
Nathan-Nesbitt / DoWhile.js
Last active May 10, 2020 00:29
Do While Example
// Initialize
var i = 0;
do {
console.log(i)
// Increment
i++;
}
// Evaluate
while (i < 10);
// Create an object car with some attributes //
var car = {
brand:"Honda",
year:2018,
kms:2500
};
// Loops through the attributes in the object
for (var attribute in car) {
console.log(attribute + ": " + car[attribute]);
@Nathan-Nesbitt
Nathan-Nesbitt / ForOfLoop.js
Created May 10, 2020 20:32
For Of Loop Example
// Create an array of fruits //
var fruits = ['Orange', 'Banana', 'Apple'];
// Loop through the values in that array
for (var fruit of fruits) {
console.log(fruit);
}
@Nathan-Nesbitt
Nathan-Nesbitt / Array.js
Created May 10, 2020 20:58
Array Example JavaScript
// Creates an array //
var array = [];
// Another way of creating an array of size 2 //
var objectArray = new Array(2);
// Creates an array with some data already in it //
var arrayWithData = [1, 2, 3, 4];
// Adds a value to the array //