Web / ES6 Interview questions
let
and const
keywords.
5.
Explain arrow function in ES6.
6.
Difference between const vs Object.freeze().
7.
What is Promise.race()?
Named Exports can export multiple values and must use the exported name when importing.
Default Exports export a single value and can use any name when importing.
ES6 was released in June 2015 as the sixth edition of the language. Initially, it was named ES6 and later renamed to ECMAScript 2015. This edition includes several new features such as modules, iterators, class, arrow functions, for...of loop, promises, and many more. Brendan Eich developed it.
ECMAScript is a specification that is defined in the ECMA-262 standard to create a general-purpose scripting language.
let
and const
keywords.
The variables declared using let
keyword will be mutable and the values can be changed at anytime. It is similar to var
keyword except that it provides block scoping.
The variables declared using the const
keyword are immutable and block-scoped. The value of the variables cannot be changed or re-assigned.
Arrow functions are the shorthand notation to write ES6 functions. The definition of the arrow function consists of parameters, followed by an arrow (=>) and the body of the function.
An Arrow function is also known as 'fat arrow' function. We cannot use them as constructors.
const functionName = (arg1, arg2) => { //body of the function }
const applies to bindings ("variables"). It creates an immutable binding, i.e., you cannot assign a new value to the binding.
Object.freeze works on values, and more specifically, object values. It makes an object immutable, i.e., you cannot change its properties.
The Promise.race() method returns a promise that fulfills or rejects as soon as one of the promises in an iterable fulfills or rejects, with the value or reason from that promise.
const promise1 = new Promise((resolve, reject) => { setTimeout(resolve, 500, 'one'); }); const promise2 = new Promise((resolve, reject) => { setTimeout(resolve, 100, 'two'); }); Promise.race([promise1, promise2]).then((value) => { console.log(value); // Both resolve, but promise2 is faster }); // expected output: "two"