Section 1

Preview this deck

const numbers = [1, 2, 3, 4]; numbers.map(n => n * n); const numbers = [1, 2, 3, 4]; numbers.map(n => { value: n });

Front

Star 0%
Star 0%
Star 0%
Star 0%
Star 0%

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Active users

0

All-time users

0

Favorites

0

Last updated

6 years ago

Date created

Mar 1, 2020

Cards (5)

Section 1

(5 cards)

const numbers = [1, 2, 3, 4]; numbers.map(n => n * n); const numbers = [1, 2, 3, 4]; numbers.map(n => { value: n });

Front

First returns squared elements in an array, second returns array of undefined objects. JS interprets curly braces as block scope of arrow function. Fix--use parentheses: numbers.map(n => ({ value: n }));

Back

for (var i=1; i<=5; i++) { setTimeout(function(){ console.log("i: " + i); },i*1000); }

Front

6 6 6 6 6 Why: there are 3 nested scopes. The anonymous function scope doesn't have access to i, nor does setTimeout, but global scope does—after it's exited the for loop. Fix: IIFE (Immediately Invoked Function Expression) for (var i=1; i<=5; i++) { (function(i){ setTimeout(function(){ console.log("i: " + i); }, i*1000); })(i); } // 1 2 3 4 5 Or, use let, which is blocked scoped: for (let i=1; i<=5; i++) { setTimeout(function(){ console.log("i: " + i); }, i*1000); } // 1 2 3 4 5

Back

let calculator = { value: 0, add: (values) => { this.value = values.reduce((a, v) => a + v, this.value); }, }; calculator.add([1, 2, 3]); console.log(calculator.value);

Front

"this" binding matches the "this" value of the enclosing lexical scope, which would be undefined or "this" in the global scope.

Back

return { value: 42 }

Front

Due to ASI (Automatic Semicolon Insertion), it's the equivalent as: return; { value: 42 };

Back

{ let firstName = "William"; var lastName = "Vincent"; // global scope! console.log(firstName + ' ' + lastName); // "William Vincent" }

Front

ReferenceError: firstName is not defined "Vincent" Variables declared with "let" or "const" are only accessible in the block scope.

Back