The .map() function can be used to convert an array of terms into an array of booleans indicating if each term is found. Then check if any of the booleans are true.
Given an array of terms:
const terms = ['term1', 'term2', 'term3'];
This line of code will return true if string contains any of the terms:
terms.map((term) => string.includes(term)).includes(true);
Three examples:
terms.map((term) => 'Got term2 here'.includes(term)).includes(true); //true
terms.map((term) => 'Not here'.includes(term)).includes(true); //false
terms.map((term) => 'Got term1 and term3'.includes(term)).includes(true); //true
Or, if you want to wrap the code up into a reusable hasTerm() function:
const hasTerm = (string, terms) =>
terms.map(term => string.includes(term)).includes(true);
hasTerm('Got term2 here', terms); //true
hasTerm('Not here', terms); //false
hasTerm('Got term1 and term3', terms); //true
Try it out:
https://codepen.io/anon/pen/MzKZZQ?editors=0012
.map() documentation:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Note:
This answer optimizes for simplicity and readability. If extremely large arrays of terms are expected, use a loop that short-circuits once a term is found.
if (str.indexOf("term1") >= 0 || str.indexOf("term2") >= 0 || str.indexOf("term3") >= 0)