There's nothing built-in that will do that for you, you'll have to write a function for it, although it can be just a callback to the some array method.

Two approaches for you:

  • Array some method
  • Regular expression

Array some

The array some method (added in ES5) makes this quite straightforward:

if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    // There's at least one
}

Even better with an arrow function and the newish includes method (both ES2015+):

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}

Live Example:

const substrings = ["one", "two", "three"];
let str;

// Setup
console.log(`Substrings: ${substrings}`);

// Try it where we expect a match
str = "this has one";
if (substrings.some(v => str.includes(v))) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(v => str.includes(v))) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

Regular expression

If you know the strings don't contain any of the characters that are special in regular expressions, then you can cheat a bit, like this:

if (new RegExp(substrings.join("|")).test(string)) {
    // At least one match
}

...which creates a regular expression that's a series of alternations for the substrings you're looking for (e.g., one|two) and tests to see if there are matches for any of them, but if any of the substrings contains any characters that are special in regexes (*, [, etc.), you'd have to escape them first and you're better off just doing the boring loop instead. For info about escaping them, see this question's answers.

Live Example:

const substrings = ["one", "two", "three"];
let str;

// Setup
console.log(`Substrings: ${substrings}`);

// Try it where we expect a match
str = "this has one";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

Answer from T.J. Crowder on Stack Overflow
๐ŸŒ
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.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_includes_array.asp
JavaScript Array includes() Method
cssText getPropertyPriority() ... fruits.includes("Banana", 3); Try it Yourself ยป ยท The includes() method returns true if an array contains a specified value....
Top answer
1 of 16
493

There's nothing built-in that will do that for you, you'll have to write a function for it, although it can be just a callback to the some array method.

Two approaches for you:

  • Array some method
  • Regular expression

Array some

The array some method (added in ES5) makes this quite straightforward:

if (substrings.some(function(v) { return str.indexOf(v) >= 0; })) {
    // There's at least one
}

Even better with an arrow function and the newish includes method (both ES2015+):

if (substrings.some(v => str.includes(v))) {
    // There's at least one
}

Live Example:

const substrings = ["one", "two", "three"];
let str;

// Setup
console.log(`Substrings: ${substrings}`);

// Try it where we expect a match
str = "this has one";
if (substrings.some(v => str.includes(v))) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (substrings.some(v => str.includes(v))) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

Regular expression

If you know the strings don't contain any of the characters that are special in regular expressions, then you can cheat a bit, like this:

if (new RegExp(substrings.join("|")).test(string)) {
    // At least one match
}

...which creates a regular expression that's a series of alternations for the substrings you're looking for (e.g., one|two) and tests to see if there are matches for any of them, but if any of the substrings contains any characters that are special in regexes (*, [, etc.), you'd have to escape them first and you're better off just doing the boring loop instead. For info about escaping them, see this question's answers.

Live Example:

const substrings = ["one", "two", "three"];
let str;

// Setup
console.log(`Substrings: ${substrings}`);

// Try it where we expect a match
str = "this has one";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

// Try it where we DON'T expect a match
str = "this doesn't have any";
if (new RegExp(substrings.join("|")).test(str)) {
    console.log(`Match using "${str}"`);
} else {
    console.log(`No match using "${str}"`);
}

2 of 16
138

One line solution

substringsArray.some(substring=>yourBigString.includes(substring))

Returns true/false if substring exists/doesn't exist

Needs ES6 support

๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ includes
String.prototype.includes() - JavaScript | MDN
Polyfill of String.prototype.includes in core-js ยท es-shims polyfill of String.prototype.includes ยท Array.prototype.includes() TypedArray.prototype.includes() String.prototype.indexOf() String.prototype.lastIndexOf() String.prototype.startsWith() String.prototype.endsWith() Was this page helpful to you?
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ javascript
JavaScript includes method for String and Array with Examples
June 2, 2023 - The includes() method is a built-in JavaScript function that checks if a specific element or substring is present in an array or a string, respectively.
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ javascript โ€บ includes-method
JavaScript includes() method: Syntax, Usage, and Examples
The includes() method in JavaScript checks whether a given value exists in a string or array. This functionality is particularly valuable in modern web development when working with data from HTML elements or applying conditional CSS styles.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ check-an-array-of-strings-contains-a-substring-in-javascript
Check If An Array of Strings Contains a Substring in JavaScript - GeeksforGeeks
April 5, 2023 - For each string, it checks if 'eks' is included using includes(). If it is, the string is pushed into the accumulator array (acc).
๐ŸŒ
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
๐ŸŒ
Love2Dev
love2dev.com โ€บ blog โ€บ javascript-includes
JavaScript Includes To Search ๐Ÿ” In Strings & Arrays
The JavaScript ๐Ÿ‘จโ€๐Ÿ’ป includes method finds ๐Ÿ” the target in an Array or String. Learn how to search arrays and strings to determine if they contain your item.
Top answer
1 of 7
1171

You really don't need jQuery for this.

var myarr = ["I", "like", "turtles"];
var arraycontainsturtles = (myarr.indexOf("turtles") > -1);

Hint: indexOf returns a number, representing the position where the specified searchvalue occurs for the first time, or -1 if it never occurs

or

function arrayContains(needle, arrhaystack)
{
    return (arrhaystack.indexOf(needle) > -1);
}

It's worth noting that array.indexOf(..) is not supported in IE < 9, but jQuery's indexOf(...) function will work even for those older versions.

2 of 7
702

jQuery offers $.inArray:

Note that inArray returns the index of the element found, so 0 indicates the element is the first in the array. -1 indicates the element was not found.

var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];

var foundPresent = $.inArray('specialword', categoriesPresent) > -1;
var foundNotPresent = $.inArray('specialword', categoriesNotPresent) > -1;

console.log(foundPresent, foundNotPresent); // true false
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>


Edit 3.5 years later

$.inArray is effectively a wrapper for Array.prototype.indexOf in browsers that support it (almost all of them these days), while providing a shim in those that don't. It is essentially equivalent to adding a shim to Array.prototype, which is a more idiomatic/JSish way of doing things. MDN provides such code. These days I would take this option, rather than using the jQuery wrapper.

var categoriesPresent = ['word', 'word', 'specialword', 'word'];
var categoriesNotPresent = ['word', 'word', 'word'];

var foundPresent = categoriesPresent.indexOf('specialword') > -1;
var foundNotPresent = categoriesNotPresent.indexOf('specialword') > -1;

console.log(foundPresent, foundNotPresent); // true false


Edit another 3 years later

Gosh, 6.5 years?!

The best option for this in modern JavaScript is Array.prototype.includes():

const found = categories.includes('specialword');

No comparisons and no confusing -1 results. It does what we want: it returns true or false. For older browsers it's polyfillable using the code at MDN.

const categoriesPresent = ['word', 'word', 'specialword', 'word'];
const categoriesNotPresent = ['word', 'word', 'word'];

const foundPresent = categoriesPresent.includes('specialword');
const foundNotPresent = categoriesNotPresent.includes('specialword');

console.log(foundPresent, foundNotPresent); // true false

๐ŸŒ
SamanthaMing
samanthaming.com โ€บ tidbits โ€บ 81-how-to-check-if-array-includes-a-value
How to check if array includes a value in JavaScript? | SamanthaMing.com
const array = [{ name: 'js' }, { name: 'css' }]; array.some(code => JSON.stringify(code) === JSON.stringify({ name: 'css' })); // true ... const array = ['SAMANTHA']; array.includes('samantha'); // false array.indexOf('samantha') !== -1; // false
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-array-includes-method
JavaScript Array includes() Method - GeeksforGeeks
JS Tutorial ยท Web Tutorial ยท ... ยท Exercise ยท Last Updated : 16 Jan, 2026 ยท The includes() method in JavaScript is used to check whether an array contains a specific value....
Published ย  June 19, 2018
๐ŸŒ
Tabnine
tabnine.com โ€บ home โ€บ how to use the includes() method in javascript
How to Use the includes() Method in JavaScript - Tabnine
July 25, 2024 - The includes() method is part of both the Array and String prototypes. This method accepts a search value as a parameter, and returns true if the value is either contained in the array on which it is called, or if it exists as a substring of ...
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ library โ€บ array โ€บ includes
JavaScript Array includes() (With Examples)
Javascript String includes() JavaScript Array splice() JavaScript Array unshift() JavaScript Array shift() Javascript String startsWith() JavaScript Array indexOf() The includes() method checks if an array contains a specified element or not. // defining an array let languages = ["JavaScript", ...
๐ŸŒ
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 two JavaScript array methods that are commonly used to find a value in an array: includes() and indexOf(). If you are checking if an array contains a primitive value, such as a string or number, the solution is more straightforward ...
๐ŸŒ
PHPpot
phppot.com โ€บ javascript โ€บ javascript-includes
How to use includes in JavaScript Array, String - PHPpot
In an array context, it checks the given value is one among the array elements. In the string prototype, the includes() in JavaScript checks if the given value is the substring.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ javascript โ€บ vt6ttbek โ€บ javascript-array-contains-example
How do I check if an array contains an element in JavaScript?
There are several ways in JavaScript to check if an element exists in an array: The includes() method: the includes() method returns a boolean indicating whether the element is present in the array;
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_includes.asp
JavaScript String includes() Method
cssText getPropertyPriority() ... text.includes("world"); Try it Yourself ยป ยท More examples below. The includes() method returns true if a string contains a specified string....
๐ŸŒ
DEV Community
dev.to โ€บ onlinemsr โ€บ javascript-includes-method-the-top-10-things-you-need-to-know-17jc
JavaScript Includes() Method: The Top 10 Things You Need to Know - DEV Community
July 8, 2023 - The includes() method returns true if it is found, and false otherwise. It performs a strict equality comparison, meaning that the value and the data type of the searched value must match the elements of the array.
๐ŸŒ
Programiz
programiz.com โ€บ javascript โ€บ library โ€บ string โ€บ includes
Javascript String includes()
JavaScript Array includes() Javascript String startsWith() Javascript String endsWith() JavaScript Number.isSafeInteger() Javascript String toLowerCase() Javascript String toUpperCase() The includes() method checks if one string can be found inside another string.