Skip to main content

How to Check if an Array Contains a Value in JavaScript with includes

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

array.includes(value) gives true when one of the array's items is exactly value, and false otherwise. It compares whole items and is case-sensitive. A string has an includes method too, but it searches text, so check that you really have an array before you rely on it.

Syntax​

Syntax
<array>.includes(<value>)
<array>.includes(<value>, <fromIndex>)
PartWhat it does
<array>The array to search.
<value>The value to look for. An item matches when it's the same value: 'green' matches 'green', but not 'Green' or 'gre'.
<fromIndex>Optional. The index to start searching at. A negative number counts back from the end.

It returns true or false. MDN's includes reference: "The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate."

The Example​

The example searches an array of three colours for 'green', for 'Green' with a capital letter, for the start of a word, 'gre', and for 'red' starting at index 1. The last line calls includes on the string 'not-green' to show how a string's includes differs.

Prerequisites​

  • Node.js. This guide uses Node.js 26.13.1.

Step 1: Write the Code​

Create a file named main.js:

main.js, which creates an array of red, green and blue, logs colors.includes for green, Green, gre, and red from index 1, then logs includes green on the string not-green.

main.js
const colors = ['red', 'green', 'blue'];

console.log(colors.includes('green'));
console.log(colors.includes('Green'));
console.log(colors.includes('gre'));
console.log(colors.includes('red', 1));

const text = 'not-green';
console.log(text.includes('green'));

Step 2: Run It​

Terminal
node main.js
Output
true
false
false
false
true

A zsh terminal where node main.js prints true, false, false, false and true.

The command is the same in zsh, bash and PowerShell.

What Happened​

  • colors.includes('green') is true, because one item is exactly 'green'.
  • 'Green' is false: the comparison is case-sensitive.
  • 'gre' is false: includes compares whole items, not the text inside them.
  • colors.includes('red', 1) is false. The search started at index 1, 'green', and 'red' is at index 0.
  • The string 'not-green' gave true for 'green'. That's String.prototype.includes, which looks for text inside the string. If code expects an array of roles and gets the string 'not-green', a check for 'green' passes.

Common Mistakes​

MistakeWhat happensDo this instead
Calling includes on a value that may be a stringIt searches text, so 'not-green'.includes('green') is trueCheck Array.isArray(value) first
Searching an array of numbers with a string, [1, 2, 3].includes('2')false, because '2' and 2 aren't the same valueConvert first, for example Number(input)
Searching for an object, [{ a: 1 }].includes({ a: 1 })false: two objects match only when they're the same objectUse some with a comparison: items.some((item) => item.a === 1)
Calling includes on null or undefinedTypeError: Cannot read properties of null (reading 'includes')Check the value is an array first

Troubleshooting​

SymptomLikely causeFix
includes says an item is missing, but you can see itA difference in case, spacing or typeCompare the exact values, for example with JSON.stringify
A check passes for a value that shouldn't passThe value was a string, not an arrayAdd Array.isArray to the check

FAQ​

What's the difference between includes and indexOf?​

includes gives true or false, while indexOf gives the item's index or -1. They also differ on NaN: [NaN].includes(NaN) is true, but [NaN].indexOf(NaN) is -1. MDN explains that includes compares with the SameValueZero algorithm, which treats NaN as equal to itself.

What does a negative fromIndex do?​

It counts back from the end: colors.includes('blue', -1) searches only the last item and gives true, and colors.includes('red', -1) gives false.

Conclusion​

includes answers "is this exact value one of the items?" in one call. Make sure the value really is an array first, because a string's includes searches text and can pass a check it shouldn't.

Additional Resources​