-
Variable Scoping
The variable scoping determines the visibility or accessibility of a variable within the certain part of the program or region.
In ES6, both const and let keywords allow developers to declare variables in the block scope.
The let statement declares a block-scoped local variable which can be reassigned. i.e, let declaration creates a mutable variable.
let a = 1;
if (a === 1) {
let a = 2;
console.log(a);
}
console.log(a);
const variables are similar to let variables but they can’t be changed through reassignment. i.e, The const declaration creates a read-only reference to a value.
const x = 1;
if (x === 1) {
const y = 2;
console.log(y);
}
console.log(x);
⬆ Back to Top
-
Arrow functions
The arrow functions provides a more concise syntax for writing function expressions by opting out the function and return keywords using fat arrow(=>) notation. Let’s see how this arrow function looks like,
var multiplyFunc = function(a, b) {
return a * b;
}
console.log(multiplyFunc(2, 5));
var multiplyArrowFunc = (a, b) => a * b;
console.log(multiplyArrowFunc(2, 5));
You can also skip parenthesis(()) if the function has exactly one parameter(either zero or more than one parameter). Apart from this, you can wrap braces({}) if the function has more than one expression in the body.
Let’s list down all the variations of arrow functions,
const message = name => console.log("Hello, " + name + "!");
message("Sudheer");
const multiply = (x, y) => x * y;
console.log(multiply(2, 5));
const even = number => {
if(number%2) {
console.log("Even");
} else {
console.log("Odd");
}
}
even(5);
const divide = (x, y) => {
if(y != 0) {
return x / y;
}
}
console.log(divide(100, 5));
const greet = () => console.log('Hello World!');
greet();
⬆ Back to Top
-
Classes
The classes are introduced as syntactic sugar over existing prototype based inheritance and constructor functions. So this feature doesn’t bring new object-oriented inheritance model to JavaScript.
There are two ways to define classes,
- Class declarations:
class Square {
constructor(length) {
this.length = length;
}
get area() {
return this.length * this.length;
}
set length(value) {
this.length = value;
}
}
- Class expressions:
const square = class Square {
constructor(length) {
this.length = length;
}
get area() {
return this.length * this.length;
}
set length(value) {
this.length = value;
}
}
You can use extend keyword to use inheritance. This enables the subclass to get all features of a parent class.
class Vehicle {
constructor(name) {
this.name = name;
}
start() {
console.log(`${this.name} vehicle started`);
}
}
class Car extends Vehicle {
start() {
console.log(`${this.name} car started`);
}
}
const car = new Car('BMW');
console.log(car.start());
Note: Even though ES6 classes looks similar to classes in other object oriented languages, such as Java, PHP, etc but they do not work exactly the same way.
⬆ Back to Top
-
Enhanced object literals
Object literals are extended to support setting the prototype at construction, shorthand for foo: foo assignments, defining methods, making super calls, and computing property names with expressions.
The important enhancements of object literals are,
- Property Shorthand:
Object’s properties are often created from variables with the same name.
Let’s see the ES5 representation
var a = 1, b = 2, c = 3,
obj = {
a: a,
b: b,
c: c
};
console.log(obj);
and it can be represented in a shorter syntax as below,
const a = 1, b = 2, c = 3;
const obj = {
a,
b,
c
};
console.log(obj);
- Method Shorthand:
In ES5, Object methods require the function statement as below,
var calculation = {
sum: function(a, b) { return a + b; },
multiply: function(a, b) { return a * b; }
};
console.log(calculation.sum(5, 3));
console.log(calculation.multiply(5, 3));
This can be avoided in ES6,
const calculation = {
sum(a, b) { return a + b; },
multiply(a, b) { return a * b; }
};
console.log(calculation.sum(5, 3));
console.log(calculation.multiply(5, 3));
- Computed Property Names:
In ES5, it wasn’t possible to use a variable for a key name during object creation stage.
var
key = 'three',
obj = {
one: 1,
two: 2
};
obj[key] = 3;
Object keys can be dynamically assigned in ES6 by placing an expression in square brackets([])
const
key = 'three',
computedObj = {
one: 1,
two: 2,
[key]: 3
};
⬆ Back to Top
-
Template literals
Prior to ES6, JavaScript developers would need to do ugly string concatenation to creat dynamic strings.
Template literals allows you to work with strings in a new way compared to ES5. These are just string literals allowing embedded expressions denoted by the dollar sign and curly braces (${expression}). Also, these literals are enclosed by the backtick (`) character instead of double or single quotes.
ES6 has two new kinds of literals:
- Template literals: string literals which exists across multiple lines and include interpolated expressions(i.e, ${expression})
const firstName = 'John';
console.log(`Hello ${firstName}!
Good morning!`);
- Tagged template literals: Function calls which are created by mentioning a function before a template literal.
The real world use case is creating components in CSS-In-JS styled components to use across the application
const Button = styled.a`
display: inline-block;
border-radius: 3px;
`
⬆ Back to Top
-
Destructuring
Destructuring is a javascript expression for extracting multiple values from data stored in objects(properties of an object) and Arrays.
Object destructuring:
This feature is used to extract values from an object.
const user = { firstName: 'John', lastName: 'Kary' };
const {firstName, lastName} = user;
console.log(firstName, lastName);
Array destructuring:
This feature is used to extract values from an array.
const [one, two, three] = ['one', 'two', 'three'];
console.log(one, two, three);
You can use destructing in below places,
- Variable declarations
- Assignments
- Parameter definitions
- for-of loop
⬆ Back to Top
-
Default parameters
Default parameters allow named parameters of a function to be initialized with default values if no value or undefined is passed.
Prior to ES6, you need check for undefined values and provide the default value for undefined values using if/else or ternary operator
function add(a, b) {
a = (typeof a !== 'undefined') ? a : 10;
b = (typeof b !== 'undefined') ? b : 20;
return a + b;
}
add(20);
add();
In ES6, these checks can be avoided using default parameters
function add(a = 10, b = 20) {
return a + b;
}
add(20);
add();
⬆ Back to Top
-
Rest parameter
The rest parameter is used to represent an indefinite number of arguments as an array. The important point here is only the function’s last parameter can be a “rest parameter”. This feature has been introduced to reduce the boilerplate code that was induced by the arguments.
function sum(...args) {
return args.reduce((previous, current) => {
return previous + current;
});
}
console.log(sum(1, 2, 3));
console.log(sum(1, 2, 3, 4));
console.log(sum(1, 2, 3, 4, 5));
⬆ Back to Top
-
Spread Operator
Spread Operator allows iterables( arrays / objects / strings ) to be expanded into single arguments/elements.
- In function and constructor calls, the spread operator turns iterable values into arguments
console.log(Math.max(...[-10, 30, 10, 20]));
console.log(Math.max(-10, ...[-50, 10], 30));
- In Array literals and strings, the spread operator turns iterable values into Array elements
console.log([1, ...[2,3], 4, ...[5, 6, 7]]);
Note: The spread syntax is opposite of rest parameter.
⬆ Back to Top
-
Iterators & For…of
String, Array, TypedArray, Map, and Set are all built-in iterables but objects are not iterables by default.
Iterators are a new way to loop over any collection in JavaScript. These are objects which defines a sequence and potentially a return value upon its termination.
An iterator implements the Iterator protocol by having a next() method that returns an object with two properties:
- value: The next value in the iteration sequence.
- done: returns true if the last value in the sequence has already been consumed.
You can make the object iterable by defining a Symbol.iterator property on it.
const collection = {
one: 1,
two: 2,
three: 3,
[Symbol.iterator]() {
const values = Object.keys(this);
let i = 0;
return {
next: () => {
return {
value: this[values[i++]],
done: i > values.length
}
}
};
}
};
const iterator = collection[Symbol.iterator]();
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
console.log(iterator.next());
for (const value of collection) {
console.log(value);
}
The for…of statement creates a loop iterating over user defined collection object. But this loop can be used for built-in objects too.
Note: The abrupt iteration termination can be caused by break, throw or return.
⬆ Back to Top
-
Generators
A generator is a function that can stop or suspend midway and then continue from where it stopped while maintaining the context(saved across re-entrances). It can be defined using a function keyword followed by an asterisk(i.e, function* ()).
This function returns an iterator object and this iterator’s next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value.
function* myGenerator(i) {
yield i + 10;
yield i + 20;
return i + 30;
}
const myGenObj = myGenerator(10);
console.log(myGenObj.next().value);
console.log(myGenObj.next().value);
console.log(myGenObj.next().value);
Note: We can use yield* to delegate to another generator function
⬆ Back to Top
-
Modules
Modules are small units of independent, reusable code to be used as the building blocks in a Javascript application.
Prior to ES6, there was no native modules support in JavaScript. There were 3 major module standards used,
- Asynchronous Module Definition (AMD)
- RequireJS Modules
- CommonJS Modules (module.exports and require syntax used in Node.js)
ES6 has provided the built-in support for modules. Everything inside a module is private by default, and runs in strict mode. Public variables, functions and classes are exposed using export statement and import the same using import statement.
Export Statement:
There are two types of exports:
- Named Exports (Zero or more exports per module)
You can export each element or a single export statement to export all the elements at once
const PI = Math.PI;
function add(...args) {
return args.reduce((num, tot) => tot + num);
}
function multiply(...args) {
return args.reduce((num, tot) => tot * num);
}
function print(msg) {
console.log(msg);
}
export { PI, add, multiply };
- Default Exports (One per module)
If we want to export a single value, you could use a default export
export default function add(...args) {
return args.reduce((num, tot) => tot + num);
}
Import Statement:
The static import statement is used to import read only live bindings which are exported by another module.
There are many variations of import scenarios as below,
import * as name from "my-module";
import { export1 } from "my-module";
import { export1 , export2 } from "my-module";
import defaultExport from "my-module";
import { export1 as alias1 } from "my-module";
⬆ Back to Top
-
Set
Set is a built-in object to store collections of unique values of any type.
let mySet = new Set()
mySet.add(1);
mySet.add(2);
mySet.add(2);
mySet.add('some text here');
mySet.add({one: 1, two: 2 , three: 3});
console.log(mySet);
console.log(mySet.size)
console.log(mySet.has(2));
⬆ Back to Top
-
Weakset
The Set is used to store any type of data such as primitives and object types. Whereas WeakSet is an object to store weakly held objects in a collection. (i.e, WeakSet is the collections of objects only). Here weak means, If no other references to an object stored in the WeakSet exist, those objects can be garbage collected.
let myUserSet = new WeakSet();
let john = { name: "John" };
let rocky = { name: "Rocky" };
let alex = { name: "Alex" };
let nick = { name: "Nick" };
myUserSet.add(john);
myUserSet.add(rocky);
myUserSet.add(john);
myUserSet.add(nick);
console.log(myUserSet.has(john));
console.log(myUserSet.has(alex));
console.log(myUserSet.delete(nick));
console.log(myUserSet.has(nick));
john = null;
⬆ Back to Top
-
Map
Map is a collection of elements where each element is stored as a Key, value pair. It can hold both objects and primitive values as either key or value and iterates its elements in insertion order.
Let’s take a map with different types of primitives and objects as key-value pairs and various methods on it,
let typeMap = new Map();
var keyObj = {'one': 1}
typeMap.set('10', 'string');
typeMap.set(10, 'number');
typeMap.set(true, 'boolean');
typeMap.set(keyObj, 'object');
console.log(typeMap.get(10));
console.log(typeMap.get('10'));
console.log(typeMap.get(keyObj));
console.log(typeMap.get({'one': 1}));
console.log(typeMap.size );
for(let item of typeMap) {
console.log(item);
}
for(let item in typeMap) {
console.log(item);
}
⬆ Back to Top
-
Weakmap
WeakMap object is a collection of key/value pairs in which the keys are weakly referenced. For this object, the keys must be objects and the values can be arbitrary values.
Let’s see various methods of weakmap with below example,
var weakMap = new WeakMap();
var obj1 = {}
var obj2 = {}
weakMap.set(obj1, 1);
weakMap.set(obj2, 2);
weakMap.set({}, {"four": 4});
console.log(weakMap.get(obj2));
console.log(weakMap.has({}));
delete obj2;
console.log(weakMap.get(obj2));
weakMap.delete(obj1)
console.log(weakMap.get(obj1));
⬆ Back to Top
-
Unicode
Prior to ES6, JavaScript strings are represented by 16-bit character encoding (UTF-16). Each character is represented by 16-bit sequence known as code unit. Since the character set is been expanded by Unicode, you will get unexpected results from UTF-16 encoded strings containing surrogate pairs(i.e, Since it is not sufficient to represent certain characters in just 16-bits, you need two 16-bit code units).
let str = '𠮷';
console.log(str.length);
console.log(text.charAt(0));
console.log(text.charAt(1));
console.log(text.charCodeAt(0));
console.log(text.charCodeAt(1));
console.log(/^.$/.test(str));
console.log('\u20BB7');
console.log(str === '\uD842\uDFB7');
ECMAScript 6 added full support for UTF-16 within strings and regular expressions. It introduces new Unicode literal form in strings and new RegExp u mode to handle code points, as well as new APIs(codePointAt, fromCodePoint) to process strings.
let str = '𠮷';
console.log('\u{20BB7}');
console.log(new RegExp('\u{20BB7}', 'u'));
console.log(/^.$/u.test(str));
console.log(str.codePointAt(0));
console.log(str.codePointAt(1));
console.log(String.fromCodePoint(134071));
⬆ Back to Top
-
Symbols
Symbol is a new peculiar primitive data type of JavaScript, along with other primitive types such as string, number, boolean, null and undefined. The new symbol is created just by calling the Symbol function. i.e, Every time you call the Symbol function, you’ll get a new and completely unique value. You can also pass a parameter to Symbol(), which is useful for debugging purpose only.
Even though equality checks on two symbols is always false, it will be true while comparing symbols with .for method due to global registry (i.e, Symbols.for(‘key’) === Symbols.for(‘key’))
These symbols are useful to uniquely identify properties or unique constants,
let id = Symbol("id");
let user = {
name: "John",
age: 40,
[id]: 111
};
for (let key in user) {
console.log(key);
}
console.log(JSON.stringify(user));
console.log(Object.keys(user));
console.log( "User Id: " + user[id] );
const logLevels = {
DEBUG: Symbol('debug'),
INFO: Symbol('info'),
WARN: Symbol('warn'),
ERROR: Symbol('error'),
};
console.log(logLevels.DEBUG, 'debug message');
console.log(logLevels.INFO, 'info message');
console.log(Symbol('foo') === Symbol('foo'));
console.log(Symbol.for('foo') === Symbol.for('foo'));
⬆ Back to Top
-
Proxies
The Proxy object is used to create a proxy for another object, which can intercept and redefine fundamental operations for that object such as property lookup, assignment, enumeration, function invocation etc. These are used in many libraries and some browser frameworks.
The proxy object is created with two parameters with below syntax,
let proxy = new Proxy(target, handler)
- target: Object on which you want to proxy
- handler: An object that defines which operations will be intercepted and how to redefine them.
The property Lookup Behavior of a user proxied object will be as below,
const target = {
name: "John",
age: 3
};
const handler = {
get: function(target, prop) {
return prop in target ?
target[prop] :
`${prop} does not exist`;
}
};
const user = new Proxy(target, handler);
console.log(user.name);
console.log(user.age);
console.log(user.gender);
These proxies also enforce value validations. Let’s take an example with set handler,
let ageValidator = {
set: function(obj, prop, value) {
if (prop === 'age') {
if (!Number.isInteger(value)) {
throw new TypeError('The age is not an integer');
}
if (value > 200) {
throw new RangeError('Invalid age');
}
}
obj[prop] = value;
return true;
}
};
const person = new Proxy({}, ageValidator);
person.age = 30;
console.log(person.age);
person.age = 'old';
person.age = 200;
⬆ Back to Top
-
Promises
A promise is an object which represent the eventual completion or failure of an asynchronous operation.
It is in one of these states:
pending: Represents initial state, neither fulfilled nor rejected.
fulfilled: Indicates that the operation is completed successfully.
rejected: Indicates that the operation is failed.
A promise is said to be settled if it is either fulfilled or rejected, but not pending. The instance methods promise.then(), promise.catch(), and promise.finally() are used to associate further action with a promise that becomes settled. And these methods also return a newly generated promise object, which can optionally be used for chaining.

The promise chaining structure would be as below,
const promise = new Promise(function(resolve, reject) {
setTimeout(() => resolve(1), 1000);
});
promise.then(function(result) {
console.log(result);
return result * 2;
}).then(function(result) {
console.log(result);
return result * 3;
}).then(function(result) {
console.log(result);
return result * 4;
}).catch(function(error){
console.log(error);
});
⬆ Back to Top
-
Reflect
Reflection is the ability of a code to inspect and manipulate variables, properties, and methods of objects at runtime. JavaScript already provides Object.keys(), Object.getOwnPropertyDescriptor(), and Array.isArray() methods as classic refection features. In ES6, it has been officially provided through Reflect object. Reflect is a new global object which is used to call methods, construct objects, get and set properties, manipulate and extend properties.
Unlike most global objects, Reflect is not a constructor. i.e, You cannot use Reflect with the new operator or invoke the Reflect as a function. It is similar to Math and JSON objects in which all the methods of this object are static.
Let’s see the usage of Reflect API with below examples,
- Creating objects using Reflect.construct();
The construct() method behaves like the regular new operator, but as a function. It is equivalent to calling new target(…args) with an option to specify a different prototype. The syntax looks like as below,
Reflect.construct(target, args [, newTarget]);
The method has below parameters,
- target: The target function to call.
- argumentsList: An array-like object specifying the arguments with which target should be called.
- newTarget: The constructor whose prototype should be used. This is an optional parameter. i.e, If newTarget is not present, its value defaults to target.
Example:
class User {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
let args = ['John', 'Emma'];
let john = Reflect.construct(
User,
args
);
console.log(john instanceof User);
console.log(john.fullName);
- Calling a function using Reflect.apply():
Prior to ES6, you can invoke a function with a specified
this value and arguments by using the Function.prototype.apply() method.
For example, you can call max() static method of Math object,
const max = Function.prototype.apply.call(Math.max, Math, [100, 200, 300]);
console.log(max);
In ES6, Reflect.apply() provides the same features as Function.prototype.apply() but in a less verbose syntax.
const max = Reflect.apply(Math.max, Math, [100, 200, 300]);
console.log(max);
- Defining a property using Reflect.defineProperty():
The
Reflect.defineProperty() method is similar to Object.defineProperty() but it returns a Boolean value indicating whether or not the property was defined successfully instead of throwing an exception.
The syntax of this method looks like below,
Reflect.defineProperty(target, propertyName, propertyDescriptor)
Let’s define the age property on user object,
class User {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return `${this.firstName} ${this.lastName}`;
}
};
let john = new User('John', 'Resig');
if (Reflect.defineProperty(john, 'age', {
writable: true,
configurable: true,
enumerable: false,
value: 33,
})) {
console.log(john.age);
} else {
console.log('Cannot define the age property on the user object.');
}
- Delete property using Reflect.deleteProperty():
The Reflect.deleteProperty() method is used to delete properties like the delete operator but as a function. It returns Boolean value indicating whether or not the property was successfully deleted.
const user = {
name: 'John',
age: 33
};
console.log(Reflect.deleteProperty(user, 'age'));
console.log(user.age);
- Get property of an object using Reflect.get():
The
Reflect.get method is used to get a property on an object like the property accessor syntax but as a function.
const user = {
name: 'John',
age: 33
};
console.log(Reflect.get(user, 'age'));
- :
⬆ Back to Top
-
Binary and Octal
ES5 provided numeric literals in octal (prefix 0), decimal (no prefix), and hexadecimal ( 0x) representation. ES6 added support for binary literals and improvements on octal literals.
1. Binary literals:
Prior to ES5, JavaScript didn’t provide any literal form of binary numbers. So you need to use a binary string with the help of parseInt()
const num = parseInt('110',2);
console.log(num);
Whereas ES6 added support for binary literals using the 0b prefix followed by a sequence of binary numbers (i.e, 0 and 1).
const num = 0b110;
console.log(num);
2. Octal literals:
In ES5, to represent an octal literal, you use the zero prefix (0) followed by a sequence of octal digits (from 0 to 7).
const num = 055;
console.log(num);
let invalidNum = 058;
console.log(invalidNum);
Whereas ES6 represents the octal literal by using the prefix 0o followed by a sequence of octal digits from 0 through 7.
const num = 055;
console.log(num);
const invalidNum = 028;
console.log(invalidNum);
Remember If you use an invalid number in the octal literal, JavaScript will throw a SyntaxError as below,
const invalidNum = 028;
console.log(invalidNum);
⬆ Back to Top
-
Proper Tail Calls
For example, the below classic or head recursion of factorial function relies on stack for each step. Each step need to be processed upto n * factorial(n - 1)
But if you use Tail recursion functions, they keep passing all the necessary data it needs down the recursion without relying on the stack.
The above pattern returns the same output as first one. But the accumulator keeps track of total as an argument without using stack memory on recursive calls.
The browsers which supports PTC do not generate stack overflow instead shows Infinity with below inputs,
This method returns the index of the first element in the array that satisfies the given test. Let’s take an example of array with all even elements except one element and use findIndex method to find the index of odd element.
ES2015/ES6 introduced a huge set of new features. But ECMAScript 2016 Or ES7 introduced only two new features:
In ES6, Promises were introduced to solve the famous callback hell problem. When a series of nested asynchronous functions need to be executed in order, it leads to a callback hell
But the Chained Promises creates complex flow for asynchronous code.
Async functions were introduced as a combination of promises and generators to give us the possibility of writing asynchronous in a synchronous manner. i.e, This function is going to be declared with the async keyword which enable asynchronous, promise-based behavior to be written in a cleaner style by avoiding promise chains. These functions can contain zero or more await expressions.
We need a straightforward way to avoid this iteration. In ES2019, Object.fromEntries() method is introduced which performs the reverse of Object.entries() behavior. The above loop can be avoided easily as below,
One of the common case of this method usage is working with query params of an URL,
While creating symbols, you also can add a description to it for debugging purposes. But there was no method to access the description directly before ES2019. Considering this, ES2019 introduced a read-only description property to retrieve a string containing the description of the Symbol.
This gives the possibility to access symbol description for different variations of Symbol objects
ES2020 is the current newer version of ECMAScript corresponding to the year 2020. This is the eleventh edition of the ECMAScript Language Specification. Even though this release doesn’t bring as many features as ES6, it included some really useful features.
Most of these features already supported by some browsers and try out with babel parser support for unsupported features. This edition is set for final approval by the ECMA general assembly in June, 2020. The ECMAScript 2020 (ES2020) language specification is ready now.
评论