Skip to content
Advertisement

multiple conditions for JavaScript .includes() method

Just wondering, is there a way to add multiple conditions to a .includes method, for example:

    var value = str.includes("hello", "hi", "howdy");

Imagine the comma states “or”.

It’s asking now if the string contains hello, hi or howdy. So only if one, and only one of the conditions is true.

Is there a method of doing that?

Advertisement

Answer

That should work even if one, and only one of the conditions is true :

var str = "bonjour le monde vive le javascript";
var arr = ['bonjour','europe', 'c++'];

function contains(target, pattern){
    var value = 0;
    pattern.forEach(function(word){
      value = value + target.includes(word);
    });
    return (value === 1)
}

console.log(contains(str, arr));
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement