fundamentals Flashcards
((false && undefined) || (false || undefined));
undefined
What does the code return?
[…Array(3)]
[undefined, undefined, undefined]
Insert an array into another array with spread syntax
let foo = [1, 2, 3]
// code here
bar; // => [1, 2, 3, 4, 5, 6, 1, 2, 3]
let bar = […foo, 4, 5, 6, …foo];
Which operator converts the non string to a string if the other operand is a string?
+
What does the code do?
Array.prototype.splice(0, 1) ;
It removes 1 element from energy, starting at index 0
What does this return?
‘4’ + 3
3 is coerced to the string ‘3’, so the result is ‘43’
What does this return?
‘fava’.charCodeAt(2)
The number of ‘v’ in the unicode: 118
undefined >= 1
// false – becomes NaN >= 1
What does this return?
1 + true
true is coerced to the number 1, so the result is 2
What happens here?
[1, 2] * 2;
[1, 2] becomes ‘1,2’, * 2 then NaN * 2 => NaN
NaN != NaN
true
When should you use try/catch/finally ?
A built in function or method can throw an error and you need to catch it
A simple guard clause is impractical
Operator after operand: (a++)
a = 1;
What is:
b = a++;
equivalent to “b = a; a++;”. so now b is 1 and a is 2
null == undefined
true
Braket notation is?
An operator. Not a method
‘11’ > 9
// true – ‘11’ is coerced to 11
Does javascript interpret a statement starting with a curly brace as an object literal?
No. it thinks it’s a block. Use parenthesis to force it to take it as an object literal
What does this return?
const str1 = ‘strunz’;
str1.endsWith(‘u’, 5);
false
How do you count all of the properties in an array, as opposed to only the indexed values?
Object.keys(array).length
Describe a mental model for how closures work
At the time of the function definition, javascript collects all the variable names that are in the function scope and places them in an ‘envelope’. This is then assigned to the function object. These variable names are pointers to the variable, as opposed to the value the variables point to. This is so that javascript can be aware of any reassignments the variables may have
What does this return? Why?
‘50’ < ‘6’
true. ‘5’ < ‘6’
There is no coercion if same type
Merge objects with spread syntax
let foo = { qux: 1, baz: 2 };
let xyz = { baz: 3, sup: 4 };
// code here
obj; // => { qux: 1, baz: 3, sup: 4 }
let obj = { …foo, …xyz };
Create a shallow clone of an array with spread syntax
let foo = [1, 2, 3];
//…. code here
foo.pop();
console.log(foo); // [1, 2]
console.log(bar); // [1, 2, 3]
let bar = […foo];
How to you return 2 to the power of 3?
Math.pow(2, 3)