Part 1 Flashcards
Must you explicitely type variables?
No,
var i = 0;
OR
i = 0;
OR
i = { }
which means you can assign them to another type.
how is var scoped
Function unless there is no function in which case it is global
how is let scoped
Block as in between the curly braces
let i = 10;
if (i == 10) { let i = 5; console.log(i); {
console.log (i);
What is the output from this?
5
10
because the let in side the curly braces is scoped within the curly braces.
Is “let” hoisted inside a function?
No, if it is referenced before it is declared, you get an error.
Example of Tagged Templates. (note back ticks)
Assume
let harry = ` Mr. and Mrs. Dursley next door `
let fred = ` Mr. and Mrs. Baggins the other next door `
let betty = ` Mr. and Mrs. Bilbo across the street`
let lines = [harry, fred, betty] ;
using a function buildHTML
function buildfHTML(strings, expr, expr1, exprLines) {
console.log(strings);
console.log(expr);
console.log(expr1);
console.log(exprLines);
}
const result = buildHTML${ lines[0] }, ${ lines[1] }, ${ exLines }
What are REST parameters?
indicates the “rest of” the parameters :-)
Input: function example( first, last, …details);
This is the opposite of “Spread” parameters.
What are spread parameters?
The opposite of REST parameters, the Spread the argument out in a statement as here:
function example(message, ...parms) { \<-- parms is rest here console.log(message, ...parms); \<-- parms is spread here
Show an example of a tail call?
If the last line of a function (the return statement) calls yet another function ( or recurses ) and then further manipulates the return from the call before returning, this is not a tail call and requires additional stack for the call.
If it does NOT manipulate the results of the call before returning, TCO (Tail call optimization) just reuses the existing call stack with out any further stack allocation.
Given variables a and b and then an array: ara = [‘first’, ‘second”],
show a destructured assignment leaving a = ‘first’, and b= ‘second’.
[a, b] = …ara;
OR
[a, b] = [‘first’, ‘second’]
[a, b, …rest] = [‘first, ‘second’, 30, 40, 50];
here rest becomes [30, 40, 50]
What happens in ES6+ for the following destructuring of variables from objects?
var course = { name: “French”, publisher: “Louis” };
function crsDetails( course ) { let { name, publisher, price } = course; ...
ES6+ at least recognizes it as an error since price can’t be set.
Show how to mark a parameter is optional in a destructured expression that follows: (price is optional)
let { name, publisher, price } = c;
let { name, publisher, ?price } = c;
Difference between for .. of and for .. in operators.
for …of use to iterate over interables (arrays).
for …in use to iterate over properties of an object.
How to retrieve value(s) from a generator such as:
function \* generatorFunction( ) { yield 'a'; yield 'b'; return 'c'; yield 'd'; }
What happens when it is called four times.
Calling
var x = generatorFunction( );
will return ‘a’, then ‘b’, then returns with the done property set to true.
What type of object is returned by a generator function?
{ value: any, done: boolean }
let s = new Set( [10, 20, 30] );
s.add( 20 );
What will happen?
20 will not be added again because it is already there.
How to remove entry ‘20’ from a set ‘s’.
s.delete( 20 );
Show using a Map iterator to get map entry contents.
m.set( ‘0’, ‘foo’ );
m.set( 1, ‘bar’ );
const iterator = m.keys();
console.log(iterator.next().value ); // “0”
console.log(iterator.next().value ); // 1
m.size; // 2