### ES 6 Interview Question and Answer - Intermediate Level

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.

// 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 2:
// undefined
// ReferenceError!
Reason: Variables declared with let doesn't gets hoisted.

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

Thank you !!

### Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters),access to the outer function’s variables and parameters, access to the global variables. Now we will look into it in detail. Consider following example
var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(123); In this example, i…

### Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting. Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post.
//Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint: Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint: Basics of hoisting //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hint: Think what happens if two functions wit…

### Javascript Interview Question and Answer - 1

In this article we are going to discuss 2 javascript interview question and their answers.These questions are highly asked in the Javascript interviews.

Disclaimer: - You might have different solution working with all test cases for both the problems.

So lets discuss the first question.This question is to test your ability in terms javascript object knowledge and your logical thinking.

Q1. You have been given a nested object "Person" which has one of the property as "password".Write a generic function to change the value of all the "password" property to "*****". Object is nested and may contain other object with property "password" and so on. Here is a sample object.

var Person = {
name: 'Alex',
age : 40,