Use JavaScript includes() method to check if the value in Array. This method returns true
if an array contains a specified value otherwise returns false
if the value is not found.
array.includes(element, start)
Note: The includes()
method is case sensitive.
JavaScript if in array
Simple example code check if string is in array js.
<!DOCTYPE html>
<html>
<body>
<script>
var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red");
var hasYellow = colors.includes("yellow");
console.log(hasRed)
console.log(hasYellow)
</script>
</body>
</html>
Output:

You can also use Array#indexOf
, which is less direct, but doesn’t require polyfills for outdated browsers.
console.log(['joe', 'jane', 'mary'].indexOf('jane') >= 0); //true
Many frameworks also offer similar methods:
- jQuery:
$.inArray(value, array, [fromIndex])
- Underscore.js:
_.contains(array, value)
(also aliased as_.include
and_.includes
) - Dojo Toolkit:
dojo.indexOf(array, value, [fromIndex, findLast])
- Prototype:
array.indexOf(value)
- MooTools:
array.indexOf(value)
- MochiKit:
findValue(array, value)
- MS Ajax:
array.indexOf(value)
- Ext:
Ext.Array.contains(array, value)
- Lodash:
_.includes(array, value, [from])
(is_.contains
prior 4.0.0) - Ramda:
R.includes(value, array)
Note: that some frameworks implement this as a function, while others add the function to the array prototype.
Do comment if you have any doubts or suggestions on this JS Array topic.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.