Javascript - Array includes() method

You can check if an element exists in an Array using this method.

Description

You can check if an element exists in an Array using this method.

It will return a Boolean value based on whether or not the element passed to it is a part of the array it is used on.

Demo

['XML', 'Screen', 'carrot'].includes('XML');    //true 
['XML', 'Screen', 'carrot'].includes('Keyboard');   //false

The above example is equivalent to:

['XML', 'Screen', 'carrot'].indexOf('XML') >= 0;    //true 
['XML', 'Screen', 'carrot'].indexOf('Keyboard') >= 0;   //false 

includes method regards all value NaN (Not a Number) as equal.

const arr = [NaN]; 
arr.includes(NaN);              // true 
arr.indexOf(NaN);               // -1 

indexOf cannot be used to check for NaN values inside an array, but the includes method allows us to check if a NaN value exists inside an array.