In this blog post, I will cover Es7 features tutorials with examples.
ECMAScript 2016 or ES2016 or Es7 introduction
- Exponentiation Operator You can also check Es6/Es2015 features.
Es7 introduced includes in the array object. includes() method check an element/object exists in an array of elements/objects.
if exists, returns true, else returns false.
includes() method is an replacement of indexOf() method of prior to es7 code.
Parameters are an element
Element to search in an array
Return value - returns the Boolean value true - element contains in Array.
Includes checks element using the sameValueZero algorithm to find an element Includes checks element using the sameValueZero algorithm to find an element
var numericArray = [9, 21, 5]; console.log(numericArray.includes(21)); // outputs true console.log(numericArray.includes(2)); // outputs false
Includes method is like of
indexOf() method of ES6 or prior versions.
var numericArray = [9, 21, 5]; console.log(numericArray.indexOf(21)>=0); // outputs true console.log(numericArray.indexOf(2)>=0); // outputs false
indexOf() method is used to check the element in an array. It returns the position of a matched element in the array or -1 if the element is not found. The developer has to write a code to check the position to return a boolean value
arr.includes(x) arr.indexOf(x) >= 0
Both methods treat +0 and -0 in the same
console.log([-0].includes(+0)) // === true) console.log([+0].includes(-0)) // === true) console.log([-0].indexOf(+0)>=0) // === true) console.log([+0].indexOf(-0)>=0) // === true)
Difference between Array.prototype.includes and Array.prototype.IndexOf method
Includes and IndexOf:
Returns Boolean value - true or false
Return numeric value >=0 or -1
Handles NaN values correctly. it will be very useful for comparing NaN values
console.log([NaN].includes(NaN))// outputs true
Inconsistency in Nan Value handling
console.log([NaN].indexOf(NaN)) // outputs false
Undefined value checks return true and comparison works as expected
console.log( [, , ].includes(undefined)>=0) // === true)
Undefined checks return false.
console.log( [, , , ,].indexOf(undefined)>=0) // === true)
Includes() makes the developer’s life easy for dealing with element comparison in the array data structure.
the name itself says it is an operator dealing with mathematical operations. The exponential operation multiplies the number by exponent times 4 exponential 2 or 4 power 2 is 4* 4=16. In Es6 and prior versions, To do exponential operations, You have to use the Math.pow method.
Es6 and Prior Version Exponentiation operator example
console.log(Math.pow(3,4)) // 81 console.log( Math.pow(5,2)) // 25
Infix Operator Example
With Es7/Es2016, New operator was introduced to handle the exponential operator Infix operator ** is used for exponential The same syntax can be rewritten using the new es7 syntax.
console.log(3**4)) // 81 console.log( 5**2)) // 25
Exponential operator assignment example
It uses as an assignment expression. Below is a code for the usage of an assignment expression.
let value = 2 value **= 5 console.log(value === Math.pow(2,5)) // true