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