
- ES6 is also known as ECMAScript 2015.
- ES6 or ECMAScript 2015 is a 6th major release of ECMAScript.
- If your browser doesn't support ES6, then you can write ES6 commpatible code using Babel and Webpack.
- You can see latest updates about ECMAScript - https://en.wikipedia.org/wiki/ECMAScript.
ECMAScript aka ES is a JavaScript standard specification to ensure the interoperability of web pages across different web browsers.
A general purpose scripting language that conforms to the ECMAScript specification.
Following are the ES6 key features that every javascript developer must know.
- Let and Const Keywords
- Arrow functions
- Template Literals
- Object Literals
- Default Parameters
- Destructuring Assignment
- Rest and Spread Operators
- Modules, Classes, Generators in ES6
- Promises in ES6
- Map and Set Data Structure
You may also like - Node.js Interview Questions Answers
ES6 introduces the new let keyword that allows us to declare variables in block scope.
let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.
Important Points:-
- In ECMAScript 2015 (ES6), let and const are hoisted but not initialized.
Referencing the variable in the block before the variable declaration results in a ReferenceError, because the variable is in a "temporal dead zone" from the start of the block until the declaration is processed. - let can not be redclare in same scope.
- const keyword used to define constants in ES6.
- it is also known as immutable variables.
- The value of a constant cannot change through reassignment, and it can't be redeclared.
In case of Object and Array we can push new items to it
Object.freeze works on values and it makes an object immutable.
Object.freeze() takes an object as an argument and returns the same object as an immutable object
Once you have frozen the object you cannot change, add or delete object's properties, but you can assign another instance.
- An arrow function has a shorter syntax than a regular function.
- In Arrow function there is no existence/binding of this, arguments, super, new.target.
- An Arrow function takes/inherit this from it's immediate parent scope (lexical scope).
- Arrow function are the best choice when working with closures and callbacks.
- Arrow function is not a good choice when working with class/object methods and constructors.
You may also like - JavaScript Interview Questions
ES6 introduce a new and easy to use string templates with placeholders for variables.
- Template literals are enclosed by the back-tick (` `).
- You can use multi-line strings.
- You can use a new syntax ${variable_name} inside of the back-ticked string.
- Before ES6 it was called Template Strings.
A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces.
Default function parameters allow named parameters to be initialized with some default values if no value or undefined is passed.
Destructuring assignment is a special syntax that allows you to "unpack" arrays or objects into a bunch of variables, as sometimes they are more convenient.
- Simple Destructuring
- Assign default value while destructuring
- Adding an Alias to a Destructured Assignment
- Adding an Alias to a Destructured Assignment
A variable can be assigned a default, in the case that the value unpacked from the array is undefined
A property can be unpacked from an object and assigned to a variable with a different name than the object property.
When destructuring an array, you can unpack and assign the remaining part of it to a variable using the rest
You may also like - React.js Interview Questions
It allows the expansion of an iterable (e.g., Array) into its constituent elements.
ORSpread operator unpacks the collected elements (iterable - Array/Object etc) into single elements.
Spread can be used in following CASE.
- Array Merge - Adding array elements to an existing array
- Copying Arrays
- Pass elements of an array to a function arguments
It is the collection of all remaining elements into an array.
Rest parameter must be the last parameter in the function. because it collects all remaining arguments into an array.
REST can be used in following CASE.
- In Function Parameter
- Destructuring the Object/Array
Important Note:-
- Rest Parameter is collecting all remaining elements into an array .
- Spread Operator is unpacking collected elements such as arrays into single elements .
Here’s a one-liner to remove duplicates from an array. (ES6, of course!)
Set was introduced in ES6 and is similar to the Sets we encounter in Mathematics: They can’t have duplicates. After we have done that, we simply convert the Set back to an array by using the spread operator.
In ES6, JavaScript classes provide the following advantage over prototype based.
- Syntactic sugar - follow OOP Approach (extends etc).
- Inheritance - JS class inheritance is still prototype-based under the hood.
- It has Static methods - which are called on the class itself only.
A generator is a function that can stop midway and then continue from where it stopped.
In short, a generator appears to be a function but it behaves like an iterator.
- yield keyword simply helps to pause and resume a function in any time asynchronously.
- Additionally it helps to return value from a generator function.
- Lazy evaluation
- Infinite sequences
- Asynchronous control flows
Promises are used to handle asynchronous operations in JavaScript.
A promise is an object which can be returned synchronously from an asynchronous function.
Promise overcome the problem of callback hell.
- Fulfilled: onFulfilled() will be called (e.g. resolve() was called).
- Rejected: onRejected() will be called (e.g. reject() was called).
- Pending: initial state, neither fulfilled nor rejected.
Map is a collection of keyed data items, just like an Object.
Note - Map allows keys of any type.
new Map()
– creates the map.map.set(key, value)
– stores the value by the key.map.get(key)
– returns the value by the key,undefined
ifkey
doesn’t exist in map.map.has(key)
– returnstrue
if thekey
exists,false
otherwise.map.delete(key)
– removes the value by the key.map.clear()
– removes everything from the map.map.size
– returns the current element count.
Set is a collection of unique data items, without keys.
new Set(iterable)
– creates the set, and if aniterable
object is provided (usually an array), copies values from it into the set.set.add(value)
– adds a value, returns the set itself.set.delete(value)
– removes the value, returnstrue
ifvalue
existed at the moment of the call, otherwisefalse
.set.has(value)
– returnstrue
if the value exists in the set, otherwisefalse
.set.clear()
– removes everything from the set.set.size
– is the elements count.
There are following differences between Arrow Function and Normal (Regular) Function:
Both Arrow Function and Normal (Regular) Function has different syntax.
In Arrow functions there are no binding of this
Unlike regular functions, arrow functions do not have their own this.
Arrow functions cannot be used as constructor with new, it will throw error.
Arrow functions don't have their own arguments object. Therefore, arguments is simply a reference to the arguments of the enclosing scope.
Arrow functions do not have a prototype property.
- Array.prototype.includes()
- Exponentiation operator
- Async/Await
- Padstart and Padend functions