You can use the .some method referenced here.

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

// test cases
const str1 = 'hi hello, how do you do?';
const str2 = 'regular string';
const str3 = 'hello there';

// do the test strings contain these terms?
const conditions = ["hello", "hi", "howdy"];

// run the tests against every element in the array
const test1 = conditions.some(el => str1.includes(el));
const test2 = conditions.some(el => str2.includes(el));
// strictly check that contains 1 and only one match
const test3 = conditions.reduce((a,c) => a + str3.includes(c), 0) == 1;

// display results
console.log(`Loose matching, 2 matches "${str1}" => ${test1}`);
console.log(`Loose matching, 0 matches "${str2}" => ${test2}`);
console.log(`Exact matching, 1 matches "${str3}" => ${test3}`);

Also, as a user mentions below, it is also interesting to match "exactly one" appearance like mentioned above (and requested by OP). This can be done similarly counting the intersections with .reduce and checking later that they're equal to 1.

Answer from dinigo on Stack Overflow
Top answer
1 of 16
418

You can use the .some method referenced here.

The some() method tests whether at least one element in the array passes the test implemented by the provided function.

// test cases
const str1 = 'hi hello, how do you do?';
const str2 = 'regular string';
const str3 = 'hello there';

// do the test strings contain these terms?
const conditions = ["hello", "hi", "howdy"];

// run the tests against every element in the array
const test1 = conditions.some(el => str1.includes(el));
const test2 = conditions.some(el => str2.includes(el));
// strictly check that contains 1 and only one match
const test3 = conditions.reduce((a,c) => a + str3.includes(c), 0) == 1;

// display results
console.log(`Loose matching, 2 matches "${str1}" => ${test1}`);
console.log(`Loose matching, 0 matches "${str2}" => ${test2}`);
console.log(`Exact matching, 1 matches "${str3}" => ${test3}`);

Also, as a user mentions below, it is also interesting to match "exactly one" appearance like mentioned above (and requested by OP). This can be done similarly counting the intersections with .reduce and checking later that they're equal to 1.

2 of 16
80

With includes(), no, but you can achieve the same thing with REGEX via test():

var value = /hello|hi|howdy/.test(str);

Or, if the words are coming from a dynamic source:

var words = ['hello', 'hi', 'howdy'];
var value = new RegExp(words.join('|')).test(str);

The REGEX approach is a better idea because it allows you to match the words as actual words, not substrings of other words. You just need the word boundary marker \b, so:

var str = 'hilly';
var value = str.includes('hi'); //true, even though the word 'hi' isn't found
var value = /\bhi\b/.test(str); //false - 'hi' appears but not as its own word
🌐
JsCraft
js-craft.io › home › javascript includes() multiple values
Javascript includes() multiple values
June 25, 2024 - Ever found yourself needing to check multiple items in a JavaScript array at once? While the includes() function is powerful, it doesn't natively support multiple values. But don't worry!
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › includes
String.prototype.includes() - JavaScript | MDN
const sentence = "The quick brown fox jumps over the lazy dog."; const word = "fox"; console.log( `The word "${word}" ${ sentence.includes(word) ? "is" : "is not" } in the sentence`, ); // Expected output: "The word "fox" is in the sentence" ... A string to be searched for within str. Cannot be a regex. All values that are not regexes are coerced to strings, so omitting it or passing undefined causes includes() to search for the string "undefined", which is rarely what you want.
🌐
TechOnTheNet
techonthenet.com › js › string_includes.php
JavaScript: String includes() method
This JavaScript tutorial explains how to use the string method called includes() with syntax and examples. In JavaScript, includes() is a string method that determines whether a substring is found in a string.
🌐
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.
🌐
Webtips
webtips.dev › webtips › javascript › string-contains-multiple-values-in-javascript
How to Check if a String Contains One of Multiple Values in JavaScript - Webtips
January 12, 2022 - const blacklist = [ 'suspicious-domain', 'untrusty-site', 'devious-page' ] blacklist.some(item => document.location.href.includes(item)) Or if you need to match against a more complex pattern, then you can combine the elements of the array into a regex object and run a test against it like so: ... const blacklist = [ 'suspicious-domain', 'untrusty-site', 'devious-page' ] const regex = new RegExp(blacklist.join('|'), 'gi') const isBlacklisted = regex.test(document.location.href) If you would like to see more webtips, follow @flowforfrank · 50 JavaScript Interview QuestionsAnd their answers explainedHere are 50 JavaScript interview questions that often come up during a technical interview.
🌐
gavsblog
gavsblog.com › home › find single or array of values in javascript array using includes
Find single or array of values in JavaScript array using includes - gavsblog
May 23, 2020 - Find out whether a JavaScript array contains single or multiple values by passing an array of values to includes() with the help of some() and every().
🌐
YouTube
youtube.com › watch
Javascript includes multiple values - YouTube
The Js array includes() function does not have a multiple values search option, but we can simulate this with the help of others array functions.📘 The full ...
Published   December 13, 2023
Find elsewhere
🌐
Tjvantoll
tjvantoll.com › 2013 › 03 › 14 › better-ways-of-comparing-a-javascript-string-to-multiple-values
Better Ways of Comparing a JavaScript String to Multiple Values
You might however find yourself needing to remember the order of the parameters (does the array come first or the value?). The check would be cleaner if the contains method were added to Array.prototype directly: Array.prototype.contains = function(obj) { return this.indexOf(obj) > -1; }; ... An often overlooked means of performing this check is to use regular expressions via String.prototype.match (docs).
🌐
Stack Abuse
stackabuse.com › bytes › javascript-check-if-multiple-values-exist-in-array
JavaScript: Check if Multiple Values Exist in Array
September 15, 2023 - let fruits = ['apple', 'banana', 'cherry', 'date']; let checkFruits = ['banana', 'date', 'grape']; let result = checkFruits.every(fruit => fruits.includes(fruit)); console.log(result); // false · Similar to our previous examples, this will check if the items in checkFruits exist in fruits. However, since we use the every() method, it will only return true if all items from checkFruits exist in fruits, which in this case is false. In this Byte, we've shown different methods to check if multiple values exist in an array in JavaScript.
🌐
Mimo
mimo.org › glossary › javascript › includes-method
JavaScript includes() method: Syntax, Usage, and Examples
Learn HTML, CSS, JavaScript, and ... functions, objects, and modern ES6+ features ... The includes() method determines whether a string or an array contains a specified value, returning true or false as appropriate....
🌐
GitHub
github.com › chaijs › chai › issues › 858
Check that string contains multiple other strings · Issue #858 · chaijs/chai
November 2, 2016 - Hello everyone! First of all, thanks for a great library ;) Second of all, please give me an advice - is there a nicer way to test that string contains multiple other strings than chaining .and.contains(...)?
Published   Nov 02, 2016
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-search-for-multiple-words-within-a-string-or-array-in-javascript
How to Search for Multiple Words Within a String or Array in ...
November 14, 2024 - To search for multiple words within a string or an array in JavaScript, you can use different approaches depending on the data structure. Here’s how to perform the search effectively: To check if a string contains multiple words, you can use String.prototype.includes(), regular expressions, or other string manipulation methods.
🌐
Vultr Docs
docs.vultr.com › javascript › standard-library › String › includes
JavaScript String includes() - Check Substring Presence | Vultr Docs
November 14, 2024 - Define a string variable to search within. Use the includes() method to check for a particular substring.
🌐
SheCodes
shecodes.io › athena › 220596-how-to-use-the-includes-method-in-javascript
[JavaScript] - How to use the .includes() method in | SheCodes
Learn how to use the `.includes()` method in JavaScript to check if a specific string or element is present in another string or array.
🌐
W3Schools
w3schools.com › jsref › jsref_includes.asp
JavaScript String includes() Method
new Set add() clear() delete() difference() entries() forEach() has() intersection() isDisjointFrom() isSubsetOf() isSupersetOf() keys() size symmetricDifference() union() values() JS Statements · break class const continue debugger do...while for for...in for...of function if...else let return switch throw try...catch var while JS Strings · at() charAt() charCodeAt() codePointAt() concat() constructor endsWith() fromCharCode() includes() indexOf() isWellFormed() lastIndexOf() length localeCompare() match() matchAll() padEnd() padStart() prototype repeat() replace() replaceAll() search() slice() split() startsWith() substr() substring() toLocaleLowerCase() toLocaleUpperCase() toLowerCase() toString() toUpperCase() toWellFormed() trim() trimEnd() trimStart() valueOf() JS Typed Arrays
🌐
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 ...