ES6 interview questions

Soumya Sunny
3 min readOct 14, 2019

--

1.What is the difference between ES6 and Javascript?

Javascript is the language. ES (Ecmascript) is the standard governing Javascript. ES6 also known as ES2015 is the version of ecmascript draft in which a lot of features were introduced (classes, arrow functions etc).

2.What are the features of ES6?

  • Let and const
  • Arrow functions
  • Spread operator, rest operator
  • Default parameter
  • Template literals
  • Classes and modules
  • Promises
  • Multiline strings
  • Loads of new methods

3. What is difference between var, let and const?

var keyword is used for declaring and initializing a variable.

let and const are ES6 features.

let has block level scope and it can be reassigned.

Const cannot be reassigned.

let and const cannot be accessed before the actual declaration is evaluated at runtime. But var is accessible.

4. What is the advantage of arrow functions?

Its is less verbose compared to the traditional function syntax. In classic function expressions, the this keyword is bound to different values based on the context in which the function is called. Whereas arrow functions use the value of this in their lexical scope.

obj = {
name: ‘Mark’,
getName: function() {
console.log(this.name);//Mark
setTimeout(function(){
console.log(this.name)//undefined
},1000);
}
}

Before ES6, we used to solve it by assigning the current context to another variable ‘that’ and used ‘that’ inside setTimeout. We can easily do this by using arrow functions.

obj = {
name: ‘Mark’,
getName: function() {
console.log(this.name); //Mark
setTimeout(()=> console.log(this.name) , 1000); //Mark
}
}

Disadvantages:

  • Arrow functions can’t be used as constructors since they don’t have prototypes.
  • Arrow functions don’t have an arguments object. But the same functionality can be achieved using rest parameters

5. What is Spread operator?

It expands value of a single variable to multiple elements or variables. It is indicated using 3 dots (…).
Examples:

Concatenation of arrays

let x=[1,2,3];
let y=[4,5,6];
let z=[…x,…y]// [1, 2, 3, 4, 5, 6]

Using math function on array

let x=[4,1,7,0];
let max=Math.max(…x);

6. What is the difference between spread and rest operators?

Spread operator expands a variable to multiple values whereas rest operator assigns remaining values of a variable to a single variable.

7. How do you swap two variables using ES6 features?

let a=1;
let b=2;
[a,b]=[b,a]

8. How do you clone an object?

  • The easiest way is using ES6
    Object.assign({},sourceObject);
  • Another solution using JSON.(It will not work for functions and date objects)
    var newObject = JSON.parse(JSON.stringify(oldObject));
  • Using for loop and hasOwnPropery
function clone(obj) {
if (obj === null || typeof(obj) !== 'object' || 'isActiveClone' in obj)
return obj;
if (obj instanceof Date)
var temp = new obj.constructor(); //or new Date(obj);
else
var temp = obj.constructor();
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
obj['isActiveClone'] = null;
temp[key] = clone(obj[key]);
delete obj['isActiveClone'];
}
}
return temp;
}

9. What is difference between Objects and map?

The key difference is that Objects only support string keys whereas Maps support more or less any key type.

10. You have three functions as given below. How do you call A and B simultaneously and C after that?

function A(){
return ("A")
}
function B(){
return ("B")
}
function C(){
return (“C”)
}

This question has multiple solutions. I am giving the simple answer below. It utilises Promise.all and promise chaining.

let a= new Promise((resolve, reject)=>{
resolve(A())
});
let b= new Promise((resolve, reject)=>{
resolve(B())
});
let c= new Promise((resolve, reject)=>{
resolve(C())
});
let result =Promise.all([a,b])
.then(msg=>{console.log(msg);
return c;
})
.then(msg1=>{console.log(msg1)})

Before you go…

If you enjoyed this story, make sure to follow me so you don’t miss my updates!

Liked this story? Don’t forget to“clap” and share to help others find it. Thank you.

You might also like Part 3- Javascript

--

--