Categories
Uncategorized

ES2016/ES7 in 5 Minutes

The ECMAScript 2016 has just two small changes.

Array.prototype.includes()

Return type: boolean

Usage

let numbers = [1, 2, 3, 4];
let arr = new Array('a', 'b', 'c', 'd');
console.log(numbers.includes(2));
console.log(numbers.includes(8));
console.log(arr.includes('a'));

Output

true
false
true

Demo

This replaces the array.indexOf(element) which returns the index if the element is found or -1 if it isnt. This new function allows you to check for NaN (Not a Number) and this works on objects in a array too, but they must match the instance being passed.

The Exponentiation Operator (**)

Usage

let num1 = 3;
let num2 = 2;
console.log(3 ** 2);

Output

9

Demo

This replaces the Math.pow(3, 2) function. This new operator allows for infix notation.