// variable
var score = 90; // old-fashioned
let name = 'Saplyn'; // preferred in modern JS
// var vs let: https://stackoverflow.com/questions/762011/what-is-the-difference-between-let-and-var
// constant
const PI = 3.14; // cannot be reassigned
// array (dictionary, associative array)
let numbers = [1, 2, 3, 4, 5];
let names = ['Saplyn', 'Moxie', 'Nyx'];
let mixed = [1, 'Saplyn', true, { name: 'Moxie' }];
// basic types
number, string, boolean
null // absence of value
undefined // uninitialized variable
symbol // guaranteed unique value type
// symbol: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol
// while loop
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
// do-while loop
let j = 0;
do {
console.log(j);
j++;
} while (j < 5);
// for loop
for (let k = 0; k < 5; k++) {
console.log(k);
}
// for-in loop (iterate over object properties)
let person = { name: 'Saplyn', age: 25 };
for (let key in person) {
console.log(key, person[key]);
}
// for-of loop (iterate over iterable objects)
let numbers = [1, 2, 3, 4, 5];
for (let number of numbers) {
console.log(number);
}
break // exit the loop
continue // skip the rest of the loop
// function declaration
function add(a, b) {
return a + b;
}
let sum = add(3, 5);
// function expression (function as value)
let greet = function(name) {
console.log(`Hello, ${name}!`);
};
greet('Saplyn');
// arrow function (ES6)
let greet = (name) => {
console.log(`Hello, ${name}!`);
};
greet('Saplyn');