Check if an Element is Present in an Array in JavaScript
To check if an element is present in a JavaScript array, use the includes()
function. includes()
was introduced in ECMAScript 2016 and is now the preferred way to look for values in JavaScript arrays.
To use it pass .includes()
after the array and provide the value to check as the first argument. Here is an example.
var array = ['a', 'b', 'c'];
result = array.includes('c');
console.log(result);
true
If the value is present includes()
will return true
otherwise, false
.