check if string in list js code example

Example 1: check value exist in array javascript

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(1, 2);  // false (second parameter is the index position in this array at which to begin searching)

Example 2: check if array contain the all element javascript

const myArray: number[] = [2, 4, 6, 8, 10, 12, 14, 16];
const elements: number[] = [4, 8, 12, 16];

function containsAll(arr: number[]) {
  return (
    arr.includes(elements[0]) &&
    arr.includes(elements[1]) &&
    arr.includes(elements[2]) &&
    arr.includes(elements[3])
  );
}
console.log(containsAll(myArray));

or you could use the following line:

function c2(arr: number[]) {
  return elements.every((val: number) => arr.includes(val));
}

Example 3: check if array does not contain string js

function checkInput(input, words) {
 return words.some(word => input.toLowerCase().includes(word.toLowerCase()));
}

console.log(checkInput('"Definitely," he said in a matter-of-fact tone.', 
["matter", "definitely"]));

Example 4: check if array does not contain string js

function checkInput(input, words) {
 return words.some(word => new RegExp(word, "i").test(input));
}

console.log(checkInput('"Definitely," he said in a matter-of-fact tone.', 
["matter", "definitely"]));