Introduction
In JavaScript, we may need to check if an array includes a value.
In this article, we'll show how to verify if an array contains a specific value.
How to check if an array includes a value in JavaScript
To check if an array includes a value in modern browsers we can use array.includes(...)
method:
The array.includes(...)
method returns true if an array contains a specific value, or false otherwise.
In older browsers we can use indexOf()
function:
If array.indexOf(...)
returns value greaten then 0 it means array includes specific value.
Conclusion
To check if an array includes a value in JS we can use the array.includes(...)
method available in modern browsers or array.indexOf(...)
available in older browsers.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}