The interview questions posted in the article are based on let, rest/spread, default parameter, destructuring, object/template literals, etc introduced in ES 6.
Find the output of the code in each question.
If you tried that same snippet but with var i in the for loop header, you'd get 5 instead of 3, because there'd only be one i in the outer scope that was closed over, instead of a new i for each iteration's function to close over.
Output 6:
Thank you !!
// Question 1
let a = 2;
if (a > 1) {
let b = a * 3;
console.log(b);
for (let i = a; i <= b; i++) {
let j = i + 10;
console.log(j);
}
let c = a + b;
console.log(c);
}
// Question 2
function foo() {
console.log(a);
console.log(b);
var a;
let b;
}
foo();
//Question 3
var funcs = [];
for (let i = 0; i < 5; i++) {
funcs.push(function() {
console.log(i);
});
}
funcs[3]();
//Question 4
var a = [2, 3, 4];
var b = [1, ...a, 5];
console.log(b);
//Question 5
function foo(x, y, ...z) {
console.log(x, y, z);
}
foo(1, 2, 3, 4, 5);
//Question 6
function foo(x = 11, y = 31) {
console.log(x + y);
}
foo();
foo(5, 6);
foo(0, 42);
foo(5);
foo(5, undefined);
foo(5, null);
foo(undefined, 6);
foo(null, 6);
//Question 7
function bar(val) {
console.log("bar called!");
return y + val;
}
function foo(x = y + 3, z = bar(x)) {
console.log(x, z);
}
var y = 5;
foo();
foo(10);
y = 6;
foo(undefined, 10);
//Question 8
var w = 1,
z = 2;
function foo(x = w + 1, y = x + 1, z = z + 1) {
console.log(x, y, z);
}
foo();
//Question 9
function foo() {
return [1, 2, 3];
}
function bar() {
return {
x: 4,
y: 5,
z: 6
};
}
var [a, b, c] = foo();
var {
x,
y,
z
} = bar();
console.log(a, b, c);
console.log(x, y, z);
//Question 10
var { x, y, z } = bar();
console.log( x, y, z );
var [,b] = foo();
var { x, z } = bar();
console.log( b, x, z );
//Question 11
function foo( [ x, y ] ) {
console.log( x, y );
}
foo( [ 1, 2 ] );
foo( [ 1 ] );
foo( [] );
function baz( { x, y } ) {
console.log( x, y );
}
baz( { y: 1, x: 2 } );
baz( { y: 42 } );
baz( {} );
Lets checkout the score. Match your solution with following answers. These question are taken from examples discussed in book You Don't know JS.
Output 1:
// 6
// 12 13 14 15 16
// 8
Reason: Variables declared with let have block scope.
Output 3:
// 3
Reason: The let i in the for header declares an i not just for the for loop itself, but it redeclares a new i for each iteration of the loop. That means that closures created inside the loop iteration close over those per-iteration variables the way you'd expect.If you tried that same snippet but with var i in the for loop header, you'd get 5 instead of 3, because there'd only be one i in the outer scope that was closed over, instead of a new i for each iteration's function to close over.
Output 4:
// [1,2,3,4,5]
Reason: In this usage, ... is basically replacing concat(..), as it behaves like [1].concat( a, [5] )here.
Output 5:
// 1 2 [3,4,5]
Reason: The ...z in this snippet is essentially saying: "gather the rest of the arguments (if any) into an array called z." Because x was assigned 1, and y was assigned 2, the rest of the arguments 3, 4, and 5 were gathered into z.
Output 6:
foo(); // 42
foo(5, 6); // 11
foo(0, 42); // 42
foo(5); // 36
foo(5, undefined); // 36 <-- `undefined` is missing
foo(5, null); // 5 <-- null coerces to `0`
foo(undefined, 6); // 17 <-- `undefined` is missing
foo(null, 6); // 6 <-- null coerces to `0`
Output 7:
foo(); // "bar called"
// 8 13
foo(10); // "bar called"
// 10 15
y = 6;
foo(undefined, 10); // 9 10
Output 8:
foo(); // ReferenceError
Output 9:
console.log(a, b, c); // 1 2 3
console.log(x, y, z); // 4 5 6
Output 10:
// 4 5 6
// 2 4 6
Output 11:
// 1 2
// 1 undefined
// undefined undefined
// 2 1
// undefined 42
// undefined undefined
Hope this article was helpful. Subscribe and leave your comments to support.Thank you !!
Comments
Post a comment