Now we’re getting to It Flashcards
JS: (expression) === VAR ? true: false;
Ternary operator evaluating for a Boolean response
JS: (expression) === VAR ? true :
(expression) ? true : false;
Nested ternary: basically if, else if, else
If the first returns false it moves into second line
JS: for (const VAR of ARRAY/STR) {
do something;
}
Basic for … of loop. Ideal to iterate thru arrays and strings
JS: const { key } = object;
console.log(key);
// prints ‘value’
A destructured assignment
Instead of
const key = object.key;
JS: const {key} = object;
key.nestedProperty;
or
key.nestedMethod();
More destructured assignment to access a nested property
.forEach() array iterates over array to return on each element but does not return a new array
JS: array.forEach(item => do something);
.map()
return a new array with the results of calling a provided function on every element of original array
const newArray = array.map(item =>
do_something);
.findIndex() to find the index of an element that matches given criteria
array.findIndex(i => i === 5);
JS: array.some()
Interator method that will check if there are ‘some’ given is criteria and return Boolean
.filter() to return a new array only with elements that pass a test
arr.filter(i => i != 5);
Returns array where i does not equal 5
.reduce() an array to a single value
array.reduce((accumulator, current) => {
return accumulator + current;
})
Short circuit evaluation (of an single if/else)
Eg.
if ‘thisVar’ exists use it,
else default to ‘that string
let variable = thisVar || ‘that string’;
Do something;
While Loop: continues while condition is true.
let i = 0;
while (i < 5) {
do_something;
i++
}
For loop. Runs a specified number of times.
for (let i = 0; i < 5; i ++) {
do _something;
}
For loop to iterate array
for (let i = 0; i < array.length; i ++) {
do_something _to _array;
}