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)
(‘a’ && (false || undefined) && ‘’);
undefined
How to check if a value is an integer?
Number.isInteger(value) => true or false
Name four side effects?
reading from the keyboard (prompt()
generating random numbers
accessing date and time
If a function can raise an exception and doesn’t catch and handle it, it has a side effect:
undefined == null
true
How do you make a quantifier lazy?
Add a ‘?’ to the quantifier. Ex. *?
Operator before operand: (++a)
a = 1
c = ++a;
equivalent to “++a; c = a;”. so now c is 2 and a is 2
What does the code log and why?
console.log(greeting);
let greeting = ‘Hello world!’;
ReferenceError: Cannot access ‘greeting’ before initialization;
Variables declared with let and const are hoisted but not initialised. They are said to live in the TEMPORAL DEAD ZONE until they are initialised to a value
What is a type error?
Trying to access a value on a property that doesn’t have any such as null.
When you call an method on an object you are accessing a value (the method) on that property
What does the regex specify?
p{3}
Exactly 3 occurrences of p
Perform Object Destructuring on the following code:
let obj = {
foo: “foo”,
bar: “bar”,
qux: 42,
};
that does the same as:
let foo = obj.foo;
let bar = obj.bar;
let qux = obj.qux;
let { foo, bar, qux } = obj;
What does the code log and why?
console.log(greeting);
var greeting = ‘Hello world!’;
undefined. var is hoisted but nothing is assigned to it. It’s like it was written like this:
var greeting;
console.log(greeting);
greeting = ‘Hello world!’;
Statements vs expressions:
Unlike expressions, statements don’t resolve to a value:
let yay;
they control the execution of a program: if else statements, for loops etc.
Statements help you do something as opposed to giving a value
What does the code log?
let ocean = {};
let prefix = ‘Indian’;
ocean.prefix = ‘Pacific’;
console.log(ocean);
{ prefix: ‘Pacific’ }
What are the associations between a name (or key) and a value in an object?
properties
Can Array.prototype.slice be passed a negative index as an argument to extract elements from the end of an array?
Yes:
[‘f’, ‘a’, ‘v’, ‘a’].slice(-2, -1); => [“v”]
[‘f’, ‘a’, ‘v’, ‘a’].slice(-2); => [“v”, “a”]
[‘f’, ‘a’, ‘v’, ‘a’].slice(-2)[0]; => ‘v’
What does this log?
let person = {
title: ‘Duke’,
name: ‘Nukem’,
age: 33
};
console.log(Object.entries(person));
[[‘title’, ‘Duke’], [‘name’, ‘Nukem’], [‘age’, 33]]
An array of sub arrays
What does this return?
0 == false //
true – becomes 0 == 0
What is an assignment?
a stand alone expression that assigns a new value to a variable
What does this return?
const str1 = ‘strunz’;
str1.startsWith(‘u’, 4);
false
11 > ‘9’
// true – ‘9’ is coerced to 9
slice() vs splice()
slice() return a portion of an array. splice() can modify an array; delete items, insert them or replace them
splice(start, deleteCount, item1, item2, itemN)
Atom shortcuts: Highlights all instances of the same word
command control g
What does the regex specify?
p{5,}
5 or more occurrences of p
how do you get the unique values from an array?
let array = [1, 1, 2, 3, 3]
[…new Set(array)]
=> [1, 2, 3]
What does this return?
1 + undefined
NaN
const animals = [‘ant’, ‘bison’, ‘camel’, ‘duck’, ‘elephant’];
console.log(animals.splice(0, 2));
console.log(animals);
[“ant”, “bison”] // the deleted items
[“camel”, “duck”, “elephant”] // the mutated array
Which data type does Number.prototype.toFixed() return?
A string
Return a random integer between 0 and 3
Math.floor(Math.random() * 4);
What does the code return?
[…Array(3)].fill(2)
[2, 2, 2]
JavaScript style guides usually recommend using which notation when possible.
dot notation. as opposed to bracket
What is an initialiser?
Looks like assignment but it’s different in terminology; it’s the expression to the right of ‘=’ in a variable declaration. ex:
let number = 35;
What is this equal to?
[ …unused, …washed ]
unused.concat(washed);
((false || undefined) || (false && undefined));
false
Why is it a bad idea to run arithmetic operators on arrays?
Besides producing useless results they run without producing a warning!
What specific examples of code can you think of that strict mode doesn’t allow?
Cannot create undeclared variables in functions so that they become global variables
Cannot start an integer with 0. Apart from 0 itself
Objects are containers for two things:
data (properties) and behaviour (methods)
undefined == ‘’
false
What does typeof null return and why?
Object. This is a historic bug. It’s too late to change it
How to get a value from an ascii code?
String.fromCharCode(68)
How does array.find() work?
It returns the first value in the array that results to true to the provided callback function:
const array1 = [5, 12, 8, 130, 44];
const found = array1.find(element => element > 10);
console.log(found);
// expected output: 12
true > null
// true – becomes 1 > 0
What does this return?
‘a’.localeCompare(‘b’);
-1
Under which conditions should you use the try, catch blocks?
1 - The built in function or method might throw an error and you need to handle it
2 - A simple guard cause is impossible or impractical
Destructure the array but only 1 and 7
let bar = [1, 2, 3, 4, 5, 6, 7];
As if you were writing;
let first = bar[0];
let seven = bar[6];
let [ first, , , , , , seventh ] = bar;
What does this return?
const str1 = ‘strunz’;
str1.startsWith(‘u’, 3);
true
What does this return?
‘’ == false
true – becomes ‘’ == 0, then 0 == 0
What is a type error?
When you call an inexistent method on an object or primitive.
What does the code return?
[…Array(3)].fill(2)
[2, 2, 2]
What is a reference error?
When you attempt to reference a variable or function that doesn’t exist
How would a regex that would match either dog, cat or rat look like and what is the type of regex called?
(cat|dog|rat) it is called ‘alternation’
Create a clone of an object with spread syntax
let foo = { qux: 1, baz: 2 };
// code here
console.log(bar); // { qux: 1, baz: 2 }
console.log(foo === bar); // false – bar is a new object
let bar = { …foo };
123 <= ‘a’
// also false
What does this return?
true == ‘1’
true – 1 == 1
Describe four (4) ways to create a date object:
new Date();
new Date(value);
new Date(dateString);
// new Date(‘December 17, 1995 03:24:00’) or
// new Date(‘1995-12-17T03:24:00’)
new Date(year, month[, date[, hours[, minutes[, seconds[, milliseconds]]]]]);
// new Date(1976, 4, other optional args);
Passing in the date object directly also works (e.g., new Date(today)). However, this employs implicit coercion which we don’t recommend. Using the Date.prototype.getTime method makes it clear what is going on.
123 > ‘a’
// false – ‘a’ is coerced to NaN; any comparison with NaN is false
let foo = false;
undefined — statements always return undefined
What is an expression?
any code that resolves to a value
What is an element of an array?
a value who has a non negative integer as it’s key (property name)
undefined === null
false
How to get a value from an ascii code?
String.fromCharCode(68)
What does the regex specify?
p{3, 7}
3 to 7 occurrences of p
How do non strict equality operators == coerce the following:
‘23’ == 23
It coerce the string to be a number
23 === 23 => true
What does the code do?
Array.from(‘foo’);
[“f”, “o”, “o”]
How do you add two decimal points to a value?
value.toFixed(2);
undefined == false
false
What does the code log?
let guess = false ? 1 : 2;
console.log(guess);
- Guess is false so it has the second argument passed to it
What is the regex for the anchor at the end of a string?
$
Use rest syntax to desctructure the following:
let foo = [1, 2, 3, 4];
console.log(bar); // 1
console.log(qux); // [2, 3, 4]
let [ bar, …qux ] = foo;
What does the code log?
let guess = true ? 1 : 2;
console.log(guess);
- Guess is true so the first argument is passed to it
What does this return?
const str1 = ‘strunz’;
str1.endsWith(‘u’, 4);
true
Name three changes strict code brings
Eliminates silent errors by throwing an exception
Prevents code than can inhibit javascript from optimising a program later on so that it runs faster
Prevents using names and syntax that may clash with future versions
Use Array destructuring for this code:
let foo = [1, 2, 3];
let first = foo[0];
let second = foo[1];
let third = foo[2];
let [ first, second, third ] = foo;
What does this return?
let friends = [‘Bob’, ‘Josie’, ‘Sam’];
let enemies = friends;
console.log(friends === enemies);
true
What does this return?
‘fava’.charAt(2);
‘v’
How does hoisting work differently depending on the type of declaration?
For a function declaration both the function name and function body get hoisted. For variables only the name does. var is initialised to undefined. let and const are said to be in a TEMPORAL DEAD ZONE until initialised and inaccessible until then
What does this return? why?
‘123’ * 3
369
all arithmetic operators other than + convert operands to numbers
What does this return?
‘0’ == false (two conversions)
true – becomes ‘0’ == 0, then 0 == 0
What does this return?
let friends = [‘Bob’, ‘Josie’, ‘Sam’];
let enemies = [‘Bob’, ‘Josie’, ‘Sam’];
console.log(friends[0] === enemies[0]);
true
What does this return?
null + true
1
What is a pragma?
A language construct that tells the compiler to process the code in a different way. It’s not part of the language
Closures are:
A lexical, rather than a runtime feature
Which are the meta characters inside a character class?
^ \ - [ ]
When is a property name an array index?
when it’s a non negative integer; (their values are called elements)
any other property name is not an array index and its associated value is not called element.
Array.prototype.indexOf returns -1 if the value it is passed is not an element of the array,
Can i code
[1, 2] + [3]
and get [1, 2, 3] ?
no. you’ll get ‘1, 23’ use .concat() instead;
[1, 2].concat([3]) // [1, 2, 3]
What does this return?
‘hello’.charAt(1);
“e”
What is a closure?
a combination of a function and it’s lexical environment