ECMAScript 2016 incorporates an includes() method for arrays that specifically solves the problem, and so is now the preferred method.

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(1, 2);  // false (second parameter is the index position in this array at which to begin searching)

As of JULY 2018, this has been implemented in almost all major browsers, if you need to support an older browser a polyfill is available.

Edit: Note that this returns false if the item in the array is an object. This is because similar objects are two different objects in JavaScript.

Answer from Alister on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_array_methods.asp
JavaScript Array Methods
Many languages allow negative bracket indexing like [-1] to access elements from the end of an object / array / string. This is not possible in JavaScript, because [] is used for accessing both arrays and objects.
Top answer
1 of 16
5530

Modern browsers have Array#includes, which does exactly that and is widely supported by everyone except IE:

console.log(['joe', 'jane', 'mary'].includes('jane')); // true

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)

Notice that some frameworks implement this as a function, while others add the function to the array prototype.

2 of 16
510

Update from 2019: This answer is from 2008 (11 years old!) and is not relevant for modern JS usage. The promised performance improvement was based on a benchmark done in browsers of that time. It might not be relevant to modern JS execution contexts. If you need an easy solution, look for other answers. If you need the best performance, benchmark for yourself in the relevant execution environments.

As others have said, the iteration through the array is probably the best way, but it has been proven that a decreasing while loop is the fastest way to iterate in JavaScript. So you may want to rewrite your code as follows:

function contains(a, obj) {
    var i = a.length;
    while (i--) {
       if (a[i] === obj) {
           return true;
       }
    }
    return false;
}

Of course, you may as well extend Array prototype:

Array.prototype.contains = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) {
            return true;
        }
    }
    return false;
}

And now you can simply use the following:

alert([1, 2, 3].contains(2)); // => true
alert([1, 2, 3].contains('2')); // => false
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_includes_array.asp
JavaScript Array includes() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST ... Array[ ] Array( ) at() concat() constructor copyWithin() entries() every() fill() filter() find() findIndex() findLast() findLastIndex() flat() flatMap() forEach() from() includes() indexOf() isArray() join() keys() lastIndexOf() length map() of() pop() prototype push() reduce() reduceRight() rest (...) reverse() shift() slice() some() sort() splice() spread (...) toReversed() toSorted() toSpliced() toString() unshift() values() valueOf() with() JS Boolean
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ includes
Array.prototype.includes() - JavaScript | MDN
The includes() method of Array instances determines whether an array includes a certain value among its entries, returning true or false as appropriate.
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ check-if-an-item-is-in-an-array-in-javascript-js-contains-with-array-includes
Check if an Item is in an Array in JavaScript โ€“ JS Contains with Array.includes()
June 28, 2022 - You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't ...
Find elsewhere
๐ŸŒ
Built In
builtin.com โ€บ software-engineering-perspectives โ€บ javascript-array-contains
JavaScript Array Contains: 6 Methods to Find a Value | Built In
Learn how to discover what a JavaScript ... multiple methods to check if an array contains a value, including indexOf, includes, some, find and findIndex....
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ javascript โ€บ how do i check if an array includes a value in javascript?
How do I check if an array includes a value in JavaScript? | Sentry
There are seven primitive data types: string, number, bigint, boolean, undefined, symbol, and null. Using the includes() method is the most readable method to check if an array contains a primitive value:
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ javascript โ€บ standard-library โ€บ Array โ€บ includes
JavaScript Array includes() - Determine Presence in Array | Vultr Docs
November 28, 2024 - The includes() method in JavaScript is a straightforward and efficient way to determine if an array contains a specific value. This approach is particularly useful when you need to check for the presence of an element without performing additional ...
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ javascript-array-includes
JavaScript Array includes() Method - Flexiple
April 25, 2024 - In summary, the JavaScript Array includes() method is an invaluable tool for efficiently verifying the presence of elements within arrays. Its simplicity and direct return of boolean values make it ideal for condition checks, data validation, ...
๐ŸŒ
Squash
squash.io โ€บ how-to-check-if-array-contains-value-in-javascript
How To Check If an Array Contains a Value In JavaScript
October 13, 2023 - The includes() method is a built-in method in JavaScript arrays that allows you to check if an array contains a specific value.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_array_search.asp
JavaScript Array Search
The findIndex() method returns the index of the first array element that passes a test function. This example finds the index of the first element that is larger than 18:
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ isArray
Array.isArray() - JavaScript | MDN
console.log(Array.isArray([1, 3, 5])); // Expected output: true console.log(Array.isArray("[]")); // Expected output: false console.log(Array.isArray(new Array(5))); // Expected output: true console.log(Array.isArray(new Int16Array([15, 33]))); // Expected output: false ... The value to be checked.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ filter
Array.prototype.filter() - JavaScript | MDN
December 13, 2025 - If no elements pass the test, an empty array is returned. The filter() method is an iterative method. It calls a provided callbackFn function once for each element in an array, and constructs a new array of all the values for which callbackFn returns a truthy value.
๐ŸŒ
STechies
stechies.com โ€บ check-array-contains-value-element-javascript
Check if Array Contains a Value or Element in JavaScript
The includes() method will return true if the JS array contains values or elements. If not, it will return false. The method will return output in a simple boolean value as includes() method is excellent for checking whether the value exists or not.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ check-if-an-element-is-present-in-an-array-using-javascript
Check if an element is present in an array using JavaScript - GeeksforGeeks
July 23, 2025 - Checking if an element is present in an array using JavaScript involves iterating through the array and comparing each element with the target value.
๐ŸŒ
ZetCode
zetcode.com โ€บ js-array โ€บ includes
JavaScript includes - checking array elements in JS
In this article we show how to check for elements using the includes method in JavaScript. The includes method determines whether an array or string contains a specified element or substring. It returns true if found, false otherwise.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ javascript โ€บ javascript-array-includes
How to Check if Array contains Specified Element in JavaScript?
December 8, 2021 - To check if an Array contains a specified element in JavaScript, call includes() method on this array and pass the element to search as argument to this