// To make a variable that can be changed
let myVariable = 'something';
// To make a variable that does not change
const myConst = 'something else';
// To make a regular string
const myStr = 'Riley Joy';
// To combine strings together
const str1 = 'My name is';
const combinedStr = myStr + ' ' + str1; // 'My name is Riley Joy'
// To interpolate (put something inside of a string)
const interpolatedStr = `My name is ${myStr}`; // 'My name is Riley Joy'
MDN Math Reference
const num1 = 12;
// Add together
const sum = num1 + 8; // 20
// Subtract
const diff = num1 - 3; // 9
// Multiply
const multiplied = num1 * 2; // 24
// Divide
const divided = num1 / 4; // 3
// True
const trueValue = true;
// False
const falseValue = false;
+
Adds together
-
Subtracts
*
Multiplies
/
Divides
=
Makes a variable equal to something
+=
Adds something to a previous variable e.g.
let num = 1;
num += 3;
// num now equals 4
-=
Subtracts something from a previous variable e.g.
let num = 3;
num -= 1;
// num now equals 2
===
One value equals another e.g. 3 === 3 // true
also 'Riley' === 'Ryan' // false
!==
One value is not equal to another e.g. 3 !== 1 // true
also 'Riley' !== 'Ryan' // true
>
One value is greater than another e.g. 4 > 3 // true
also 4 > 5 // false
>=
One value is greater than or equal to another e.g. 4 >= 3 // true
4 >= 4 // true
<
One value is less than another e.g. 4 < 3 // false
also 4 < 5 // true
<=
One value is less than or equal to another e.g. 4 <= 3 // false
also 4 <= 4 // true
? :
Ternary comparison e.g. const amIDaddy = myName === 'Ryan' ? 'I am daddy!' : 'I am not daddy.'
&&
The and operator is used inside comparisons e.g.
const amIDaddy = myName === 'Ryan' && myAge > 34 ? 'I am daddy!' : 'I am not daddy.'
||
The or operator is used inside comparisons
const amIDaddy = myName === 'Ryan' || myName === 'Rand' ? 'I am daddy!' : 'I am not daddy.'
let isGirl = false;
// Using if
if(myName === 'Riley') {
isGirl = true;
} else if(myName === 'Amity') {
isGirl = true;
} else if(myName === 'Piper') {
isGirl = true;
} else {
isGirl = false;
}
// Using if with && operator
if(myName === 'Riley' && myAge >= 13) {
isGirl = true;
} else {
isGirl = false;
}
// Using if with || operator
if(myName === 'Riley' || myName === 'Amity' || myName === 'Piper') {
isGirl = true;
} else {
isGirl = false;
}
// Using switch
switch(myName) {
case 'Riley':
isGirl = true;
break;
case 'Amity':
isGirl = true;
break;
case 'Piper':
isGirl = true;
break;
default:
isGirl = false;
}
// Declaring a function
// This is NOT what you want to use in real life
// You want to use function expressions in real code
function friends(friend1, friend2) {
console.log(`${friend1} and ${friend2} are friends.`);
}
// Calling the function
friends('Riley', 'Vivian'); // logs "Riley and Vivian are friends."
// Creating a function expression
const printName = function(name) {
console.log(`My name is ${name}!`);
};
// Calling the function
printName('Riley Joy'); // logs "My name is Riley Joy!"
// Creating a multiline arrow function
const addNumbers = (num1, num2) => {
return num1 + num2;
};
// Calling the function
addNumbers(3, 4) // returns 7
// Creating a single line arrow function
const multiplyNumbers = (num1, num2) => num1 * num2;
// Calling the function
multiplyNumbers(4, 5); // returns 20
// Make an array
const myArr = [];
// Make an array with strings inside
const myStrArr = ['one', 'two', 'three'];
// Make an array with numbers inside
const myNumArr = [1, 2, 3];
// Push a new item into an array
const names = ['Riley', 'Amity', 'Piper', 'Isaac'];
names.push('Jeff');
// ['Riley', 'Amity', 'Piper', 'Isaac', 'Jeff'];
// Remove an item from the end of an array
names.pop();
// ['Riley', 'Amity', 'Piper', 'Isaac'];
// Slice an array
const girls = names.slice(0, 3);
// ['Riley', 'Amity', 'Piper'];
// Concatonate (join together) arrays
const girls = ['Riley', 'Amity', 'Piper'];
const boys = ['Isaac', 'Jeff'];
const kids = girls.concat(boys);
// ['Riley', 'Amity', 'Piper', 'Isaac', 'Jeff'];
// Spread arrays
const parents = ['Ryan', 'Hannah'];
const family = [...parents, ...kids];
// ['Ryan', 'Hannah', 'Riley', 'Amity', 'Piper', 'Isaac', 'Jeff'];
// Get item from an index
const fourthChild = kids[3];
// 'Isaac'
// Change item using an index
kids[3] = 'Isaac Fain';
// Nested array
const names = [
['Ryan', 'Scott'],
['Hannah', 'Joy'],
['Riley', 'Joy']
];
// Get item using indexes
const hannahMiddleName = names[1][1];
const rileyFirstName = names[2][0];
const rileyMiddleName = names[2][1];
// Update item using indexes
names[0][1] = 'Manjoy';
// For loop
for(let i = 0; i < 5; i++) { // starts at 0 and adds 1 with each loop then stops at 5
console.log(i);
}
// logs 0 1 2 3 4
for(let i = 5; i > -1; i--) { // starts at 5 and subtracts 1 with each loop then stops at -1
console.log(i);
}
// logs 5 4 3 2 1 0
// For of loops
const daughters = ['Riley', 'Amity', 'Piper'];
for(const daughter of daughters) {
console.log(`My favorite daughter is ${daughter}!`);
}
// While loops
let x = 0;
let sum = 0;
while(x < 10) {
sum = sum + x;
x++;
}
console.log(sum); // logs 45