ES6 Javascript Flashcards

1
Q

<p>Must you explicitely type variables?</p>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

<p>how is var scoped</p>

A

<p>Function unless there is no function in which case it is global</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

<p>how is let scoped</p>

A

<p>Block as in between the curly braces</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

<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>

A

<p> 5<br></br> 10</p>

<p>because the let in side the curly braces is scoped within the curly braces.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

<p>Is "let" hoisted inside a function?</p>

A

<p>No, if it is referenced before it is declared, you get an error.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

<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>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

<p>What are REST parameters?</p>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

<p>What are spread parameters?</p>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

<p>Show an example of a tail call?</p>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

<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>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

<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>

A

<p>ES6+ at least recognizes it as an error since price can't be set.</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

<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>

A

<p></p>

<p> let { name, publisher, <strong>?</strong>price } = c;</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

<p>Difference between for .. of and for .. in operators.</p>

A

<p>for ...of use to iterate over interables (arrays).</p>

<p>for ...in use to iterate over properties of an object.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

<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>

A

<p>Calling</p>

<p> var x = generatorFunction( );</p>

<p>will return 'a', then 'b', then returns with the done property set to true.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

<p>What type of object is returned by a generator function?</p>

A

<p>last Yielded value or null if done </p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

<p> let s = new Set( [ 10, 20, 30 ] );</p>

<p> s.add( 20 );</p>

<p>What will happen?</p>

A

<p>20 will not be added again because it is already there.</p>

<p></p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

<p>How to remove entry '20' from a set 's'.</p>

A

<p> s.delete( 20 );</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

<p>Show using a Map iterator to get map entry contents.</p>

A

<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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

<p>Show how to set a map entry.</p>

<p>Console.log( show if value was set );</p>

A

<p> myMap = new Map();<br></br>... myMap.set( name<strong>,</strong>'first' );</p>

<p> console.log( myMap.has(keyvalue) ); // true of false</p>

20
Q

<p>show how to empty a map.</p>

A

<p>map.clear( );</p>

21
Q

<p>show how to delete a specific map entry</p>

A

<p>map.delete( key );</p>

22
Q

<p>Show how to get the value of a map given a key</p>

A

<p>myValue = map.get( key );</p>

23
Q

<p>Show how to test for a specific key value in a map.</p>

A

<p> map.has( key );</p>

24
Q

<p>show how to interate over a map and show the key, value, and map.</p>

A

<p> var m = new Map( );<br></br> m.set( 'a', 1 );<br></br> m.set( 'b', 2 )</p>

<p> m.forEach ( (k, v, m) =><br></br> console.log( <strong>`</strong>key: ${ k } value: ${ v } map: ${ m }<strong>`</strong>) )</p>

<p>// key:1 value:a map:[object Map]<br></br>// key:2 value:b map:[object Map]</p>

25
Q

<p>show using for .. of to iterate a map showing key and value</p>

A

<p>for ( [key,value] of m )<br></br> console.log( key + '=' + value )</p>

<p>// a=1<br></br>// b=2</p>

26
Q

<p>Show how to use an map iterator to obtain the <strong>entries</strong>.</p>

A

<p>var m = new Map( )<br></br>m.set( 'a', 1 )<br></br>m.set( 'b', 2 )</p>

<p>var iter = m.<strong>keys</strong>( )<br></br>obj = iter.next(); // obj: {value: "a", done: false}<br></br>obj =iter.next(); // obj: {value: "b", done: false}<br></br>obj =iter.next(); // obj: {value: undefined, done: true}</p>

27
Q

<p>Show how to use an iterator with a Map to obtain the <strong>values</strong>.</p>

A

<p>var m = new Map( )<br></br>m.set( 'a', 1 )<br></br>m.set( 'b', 2 )</p>

<p>var iter = m.<strong>values</strong>( )<br></br>obj = iter.next(); // obj: {value: "a", done: false}<br></br>obj =iter.next(); // obj: {value: "b", done: false}<br></br>obj =iter.next(); // obj: {value: undefined, done: true}</p>

28
Q

<p>Describe a MAP function</p>

A

<p>Objects have been used as maps (key value pairs).</p>

<p>Keys can be any type and are placed in the order they were inserted.</p>

<p>Diff between Map and Object</p>

<p>Objects must be string keys, Maps any type<br></br>Objects are now ordered, but you must find the keys and then iterate.</p>

<p>Maps have a size function.</p>

<p>Example Map:<br></br>let contacts = new Map()<br></br> contacts.set('Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"})<br></br> contacts.has('Jessie') // true<br></br> contacts.get('Hilary') // undefined<br></br> contacts.set('Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}) contacts.get('Jessie') // {phone: "213-555-1234", address: "123 N 1st Ave"}<br></br> contacts.delete('Raymond') // false<br></br> contacts.delete('Jessie') // true<br></br> console.log(contacts.size) // 1</p>

29
Q

<p>Show example of jQuery and AJAX. ( commonly used pre ES6 )</p>

A

<p>$.ajax("http://services.odata.org/v4/Northwind/Northwind.svc/", <strong>{</strong><br></br> success: function(data)<strong> {</strong><br></br> console.log(data.value);<br></br> <strong>},</strong><br></br> error: function( ) <strong>{</strong><br></br> console.log("failed to fetch data") ;<br></br> <strong>}</strong><br></br><strong>} ) ;</strong></p>

30
Q

<p>Show example of using ES6+ fetch method.</p>

A

<p>fetch( "http://services/odata.org/V4/Northwind/Northwind.svc/", {<br></br> method: 'get' } ) <strong>.</strong> then( function(response) {<br></br> return response.json()<strong>;</strong><br></br> } ) <strong>.</strong> then( function(data) {<br></br> console.log( data.value) <strong>;</strong><br></br> } ) <strong>. </strong>catch( function( ) {<br></br> console.log("failed to fetch data)<strong>;</strong><br></br> } ) <strong>;</strong></p>

31
Q

<p>Show a promise constructor.</p>

A

<p>function add_positivenos_async(n1, n2)<br></br>{<br></br> let p = new Promise(function (resolve, reject) {<br></br> if (n1 >= 0 &amp;&amp; n2 >= 0) { //do some complex work<br></br> resolve(n1 + n2) }<br></br> else<br></br> reject('NOT_Postive_Number_Passed')</p>

<p> } )<br></br> return p;<br></br>}</p>

<p>add_positivenos_async(10, 20)<br></br> <strong>.</strong>then(successHandler) // if promise resolved<br></br> <strong>.</strong>catch(errorHandler);// if promise rejected</p>

<p>add_positivenos_async(-10, -20)<br></br> <strong>.</strong>then(successHandler) // if promise resolved<br></br><strong> .</strong>catch(errorHandler);// if promise rejected</p>

<p>function errorHandler(err) {<br></br> console.log('Handling error', err)<br></br>}</p>

<p>function successHandler(result) {<br></br> console.log('Handling success', result)<br></br>}</p>

32
Q

<p>Show using promise to wrap a jQuery Ajax pattern.</p>

A

<p>var promise = new Promise( resolve, reject ) {</p>

<p> $.ajax( "http://services.odata.org/V4/Northwind/Northwind.svc/"<strong>,</strong> {<br></br> success: function (data) {<br></br> resolve(data);<br></br> } ,<br></br> error: function( ) {<br></br> reject("Error");<br></br> }<br></br>}</p>

<p>promise.then (function (result) {<br></br> console.log( result );<br></br>} , function ( err ) {<br></br> console.log( err );<br></br>} ) ;</p>

33
Q

<p>Show example of string repeat</p>

A

<p>var stg = "SkillBakery ".repeat(2);</p>

<p>stg is "SkillBakery SkillBakery ";</p>

34
Q

<p>String contains has been deprecated. Show how to determine if a string contains a substring.</p>

A

<p>result: bool = " World ".includes( "rl" ) ;</p>

<p>true</p>

35
Q

<p>What is the method to test the starting of a string.</p>

A

<p>var x:bool = "SkillBakery ES6" <strong>.</strong> startsWith( "Skill" ) <strong>;</strong></p>

<p><strong>true</strong></p>

36
Q

<p>What is the method to test the ending of a string.</p>

A

<p>var x:bool = "SkillBakery ES6" . endsWith( "Bakery" ) <strong>;</strong></p>

<p><strong>false</strong></p>

37
Q

<p>Iterate over a string.</p>

A

<p>for ( var ch of 'SkillBakery') {<br></br> console.log( ch ) <strong>;<br></br>}</strong></p>

38
Q

<p>Show each:<br></br> how to get the part of the number that left of decimal</p>

<p> test if the number IS a number or not</p>

<p> test if a number if finite or not.</p>

A

<p></p>

<p>Math.trunc(39.7) // 39</p>

<p>Number.isNaN(45) // false</p>

<p>Number.isFinite(-Infinity) // false</p>

<p></p>

39
Q

<p>How to search an array of strings for a specific entry?</p>

A

<p>var result = [ "skill", "bakery", "es6", "course" ]. find(x => x == "Bakery");</p>

40
Q

<p>Show how to create an Array using the characters in a string.</p>

A

<p>var myA = Array.from("this is a test");</p>

<p></p>

41
Q

<p>Show creating an array using passed arguments.</p>

A

<p>var A = Array.of(42, 16, 20);</p>

42
Q

<p>Show how to overwrite an array from a starting position.</p>

A

<p> [ 0, 0, 0] . fill( 7, 1 ) starts at position 1: [ 0, 7, 7 ]</p>

43
Q

<p>Find the index of an entry in an Array.</p>

A

<p> [ 1, 2, 3, 4, 5 ] <strong>.</strong>findIndex(x => x == 2)</p>

<p> result is 1</p>

<p></p>

44
Q

<p>Show how to copy (overwriting) an array within.</p>

A

<p> [ 1, 2, 3, 4, 5, 6] <strong>.</strong>copyWithin( 3, 1 )</p>

<p> says find the entry with value '3' and begin copying AFTER that using the source starting with index == 1 (value '2').</p>

<p>result: [ 1, 2, 3, 2, 3, 4]</p>

45
Q

<p>Show combining one or more source <strong>objects</strong>.</p>

A

<p>var destination = { start: 0 };<br></br>var source_mid = { interval: 10, duration: 20 } ;<br></br>var source_end = { distance: 40 };<br></br><br></br>Object.assign( destination, source_mid, source_end ) ;</p>

<p><strong>destination</strong>is the concatenation of itself and the other arguments.</p>

<p>subsequent changes to sources, do not affect the new destination entries.</p>

46
Q

<p>What is a "template literal"?</p>

A

<p>ES5 they were called template strings.</p>

<p>They are string literals showing embedded expressions.</p>

<p>`string text`</p>

<p> `string text line 1<br></br> string text line 2` </p>

<p> `string text ${expression} string text`</p>

<p>----------------------------------------------------------------------------------</p>

<p> tag`string text ${expression} string text`</p>

<p>If there is an expression preceding the template literal (taghere), this is called a<strong>tagged template</strong>. In that case, the tag expression <strong>(usually a function)</strong> gets called with the template literal, which you can then manipulate before outputting.<br></br></p>