ES7 features in Javascript

                                   

What is ES7?

The previous version (ES6), provided a lot of major features like arrow function sets, maps, classes and destructuring, and much more. It provided a completely new look for our Javascript code. Javascript ES7 is an updated version of ES6 which includes specific features:-

1)Array.includes()

2)Exponentiation operator

3)Exponential Assignment Operator

4)Object Properties

Understanding the concepts-

Array.includes()

Array.prototype.includes() is the newly added feature of ES7 that checks the array whether the value passed as an argument is present in it or not. It returns true if the array contains the value, otherwise, it returns false.   

Syntax             

 Array.includes(value)                                                                                                                    

 OR                                                                                                       

Array.includes(value,start_index)

In the second syntax, it will start searching from that particular index.

Let’s start with an example to understand it better.

let numbers = [1, 2, 3, 4];                                                                       

numbers.includes(2) //returns true .

Earlier when we used .indexOf() property, it also helped in finding if a particular value is present or not, but it had a drawback.

indexOf() property couldn’t handle NaN well.

let numbers = [1, 2, 3, 4, NaN];

Now in the above case, let’s see the difference in answers we receive from both methods.

numbers.indexOf(NaN)  //returns -1=>false

numbers.includes(NaN)  //returns true 

Let’s start with the next feature.   

Exponentiation Operator

JavaScript already supports many arithmetic operators like +, -, *, and more.

ECMAScript 2016(ES7) introduced the new feature-exponentiation operator, **.

It has the same purpose as Math.pow(). It returns the first argument raised to the power of the second argument.

Syntax:-if there is a base and exponent(exp), it can be written as-(base**exp) it is the same as Math.pow(base,exp).

Example-let base=2,exp=4,base**exp=16.

It is the same as Math.pow(2,4)=16

Exponential Assignment Operato

This property is also similar to Math.pow() method. You can say, it is like using Math.pow() and assigning the new value to the original base value. Let’s further try to understand it. If we are assigning base as a base to the power exp, we can write it as base=base**exp. Now, the above expression with the new ES7 feature can be written as:- base**=exp.

Object Properties

ES7 introduced new properties to play with objects. Here we are going to learn about 2 new features:-                             

i) Object.values():-It returns the values present in JSON Object.

Example:-let  obj_json={obj1: 10,obj2: 20,obj3: 30,obj4: 40}

let result=Object.values(obj_json).map((i)=>{return i})

console.log(result) //returns [ 10, 20, 30, 40 ]

ii) Object.entries():-It will fetch all the entries present in a particular object.

Let’s learn in the form of an example:-

var obj_json = {obj1: 10,obj2: 20,obj3: 30,obj4: 40}

Object.entries(obj_json).map((i)=>{console.log(i)})

//returns 

[‘obj1’, 10 ]

[‘obj2’, 20 ]

[‘obj3’, 30 ]

[‘obj4’, 40 ]

Summary

In this article, we learned about ECMAScript 2016 or ES7, which defines the new features for JavaScript implementation. ES7 provides Array.includes(), Exponentiation operator, Exponential Assignment Operator, Object Properties, and many more features that make working with JavaScript a lot easier.

Leave a Comment