Functions
For these exercises, always return
the answer!
function adder(first, second) {
let theAnswer = first + second;
return theAnswer;
}
Relational Operators
Symbol |
Meaning |
=== |
exactly equals |
!== |
does not equal |
= |
assignment |
+= |
add or concatenate |
-= |
subtract |
* |
multiplication |
/ |
division |
% |
remainder (modulo) |
> |
greater than |
>= |
greater than or equal |
< |
less than |
<= |
less than or equal |
Logical Operators
Symbol |
Meaning |
! |
not |
&& |
and |
|| |
or |
If / Else If / Else
let someNumber = prompt("Pick a number");
if (someNumber < 42) {
// something
}
else if (someNumber === 42) {
// another thing
}
else {
// some default action
}
For Loop - When You Need the Index Value
let nums = [2, 7, 8, 6, 5];
for (let i = 0; i < nums.length; i++) {
console.log(nums[i]);
}
// 2
// 7
// 8
// 6
// 5
For Loop - Don't Need the Index Value
let nums = [2, 7, 8, 6, 5];
for (let number of nums) {
console.log(number);
}
// 2
// 7
// 8
// 6
// 5
Arrays
let nums = [42, 13, 78];
nums[0]; // 42
nums[2]; // 78
nums.length; // 3
nums.push(99);
// now nums = [42, 13, 78, 99]
let last = nums.pop();
// now last = 99 and nums = [42, 13, 78]
let first = nums.shift();
// now first = 42 and nums = [13, 78]
nums.unshift(55);
// now nums = [55, 13, 78]
For more info, check the MDN Reference
Math Functions
let nearestInt = Math.round(1.55);
// nearestInt is now 2
let nextLowestInt = Math.floor(1.55);
// nextLowestInt is now 1
let nextHighestInt = Math.ceil(1.2);
//nextHighestInt is now 2
Maps
//create a map
let englishToFrench = new Map();
//add new key/value pair
englishToFrench.set("hello", "bonjour");
// retrive a value via the key
let word = englishToFrench.get("hello");
// word is now set to "bonjour"
// check if a Map contains a key
englishToFrench.has("hello");
// would return true
// remove a key/value pair from the map
englishToFrench.delete("hello");
For more info, including using a forEach loop,
check the MDN Reference
Strings
let name = "Navaneeth";
// determine length
console.log(name.length); //9
// access a specific character in a string
let letter = name[2];
// letter is now set to "v"
// start counting index values from 0
// access a slice of a string
let part = name.substring(2, 5);
// part is now set to "van"
// from 2 go up to, but not including, 5
// build and concatenate a string
let someone = "Alastor";
someone += " Moody";
//someone is now set to "Alastor Moody"
For more info check the MDN Reference