How do I check if an array includes a value in JavaScript?

Created by codeapeLinked to 64.4m issues across 272 teams


Modern browsers have Array.includes, which checks if an array contains a certain value and is widely supported by all browsers except IE. Additionally, Array.indexOf can be used, which is less direct but does not require polyfills for outdated browsers. Many frameworks also offer similar methods, such as jQuery's $.inArray(value, array, [fromIndex]) and Underscore.js's _.contains(array, value).

console.log(['joe', 'jane', 'mary'].includes('jane')); //true
We use cookies to personalize content
run ads, and analyze traffic.