Before es2016, we have to use indexOf() to check if a value is contained in an array. Now we have includes which is similar to indexOf() but more convenience in some case. Let check it out.
The Array method includes has the following signature:
Array.prototype.includes(value: any) : boolean
It return true if value is an element of the array, otherwise return false.
['a', 'b', 'c'].includes('a'); // => true [1, 2, 3].includes(4); // => false
includes is similar to indexOf for most of cases. The two expressions below are equivalent:
arr.includes(x) arr.indexOf(x) >= 0
Slightly difference between includes and indexOf() is includes check N/A, whereas indexOf doesn't
[NaN].includes(NaN); // => true [NaN].indexOf(NaN); // => -1 [ , , ].includes(undefined) // => true [ , , ].indexOf(undefined) // => -1
Before es2016, to calculate value of 2 to power of 3 we have to use Math.pow(2,3). But now we can do this more easily and straightforward by using exponent operator
2 ** 3; // => 8 Math.pow(2,3); // => 8