Since 2015, JavaScript has improved immensely.
It’s much more pleasant to use it now than ever.
In this article, we’ll look at the best features of ES2016.
Array.prototype.includes
The includes
array instance method is added in ES2016.
It lets us check if an item exists in the array.
For instance, we can write:
[1, 2, 3].includes(1)
then that returns true
.
If we have:
[1, 2, 3].includes(6)
then it returns false
.
It takes the item we want to check and returns a boolean.
includes
is similar to indexOf
.
So:
arr.includes(x)
is mostly the same as:
`arr.indexOf(x)` `>=` `0`
The main difference is that includes
can check for NaN
, but indexOf
can’t.
So if we write:
[NaN].includes(NaN)
then that returns true
.
But if we have:
[NaN].indexOf(NaN)
then that returns -1.
includes
doesn’t distinguish between +0 and -0.
So if we write:
[-0].includes(+0)
then that returns true
.
Exponentiation operator (**
)
The exponentiation operator is added to ES2016.
It lets us do exponentiation without calling Math.pow
.
For instance, if we have:
3 ** 2
we get 9.
So:
`x` `**` `y`
does the same thing as:
Math.pow(x, y)
We can use **
with =
.
So we can write:
let num = 3;
num **= 2;
then num
is 9.
Precedence
The exponentiation operator binds very strongly.
So if we have:
2**2 * 2
then that’s the same as:
(2**2) * 2
It binds more strongly than *
.
And *
binds more strongly than **
.
Conclusion
ES2016 is a small update to ES2015, which was a big release.
It gives everyone a break on new features for a year.
New features include the array instance includes
method and the exponentiation operator.