/ JavaScript

ES2016/ES7 Features

ES2015 was significant concerning the features it brought to the table compared to the ES2016. Therefore, we didn't hear much about what ES2016 has introduced. So here we will discuss those two features: Array.prototype.includes and the exponentiation operator **.

The Array.prototype.includes Method

The first feature which was mainly proposed to be Array.prototype.contains due to the problems that it may introduce, "including" breaking MooTools library that tons of websites depend on. The method returns true if the value that was passed to it belongs/is an element of the array. It has the following signature:

Array.prototype.includes(value : any) : boolean

Some examples of usage.

['a', 'b', 'c'].includes('a'); // true

['a', 'b', 'c'].includes('d'); // false

An optional argument fromIndex allows searching from a specific index.

let letters = ['a', 'b', 'c', 'd', 'e', 'f'];

letters.includes('d', 1); // true
letters.includes('d', 3); // true
letters.includes('d', 4); // false

The method requires that the searched element does match the type not only the value. It uses a slightly modified version of strict equality.

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

numbers.includes(1); // true 
numbers.includes(2); // false  

A negative from index parameter could be passed as well which let the search starts from that index added to array.length and till the end of the array.

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

numbers.includes(2, -2); // false
numbers.includes(4, -2); // true

include method will find a NaN introduced in an array.

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

numbers.includes(NaN); // true

The Exponentiation Operator **

This operator is a short version of Math.pow(x, y), if you are a Python dev, you might recognize this.

5 ** 2; // 25
3 ** 2; // 9

We can also combine the exponentiation with an assignment like so **=.

let num = 3;

num **= 2; //9

It is worth to mention that any number, including NaN, powered to 0 exponent evaluates to 1. And If the exponent is NaN, the result is always NaN, for any base value.

99 ** 0; // 1
NaN ** 0; // 1
Infinity ** 0; // 1

99 ** NaN; // NaN
-99 ** NaN; // NaN
NaN ** NaN; // NaN
Anas Shekhamis

Anas Shekhamis

A software engineer who specializes in architecting and developing web applications. On a daily basis, I use Python, JavaScript, Ruby, and PHP to design and implement API's and build web applications.

Read More
ES2016/ES7 Features
Share this