.includes javascript code example

Example 1: javascript string contains

var string = "foo",
    substring = "oo";

console.log(string.includes(substring));

Example 2: javascript string includes

'Blue Whale'.includes('blue')  // returns false

Example 3: js string contains

var string = "foo";
var substring = "oo";

console.log(string.indexOf(substring) !== -1);

Example 4: js array includes

[1, 2, 3].includes(2);     // true
[1, 2, 3].includes(4);     // false
[1, 2, 3].includes(3, 3);  // false
[1, 2, 3].includes(3, -1); // true
[1, 2, NaN].includes(NaN); // true

Example 5: javascript includes

const pets = ['cat', 'dog', 'bat'];

console.log(pets.includes('cat'));
// output: true

Example 6: .includes javascript

let storyWords = ['extremely literally actually hi bye okay']
let unnecessaryWords = ['extremely', 'literally', 'actually' ];

let betterWords = storyWords.filter(function(word) {
  return !unnecessaryWords.includes(word);
});
console.log(betterWords) // ['hi' 'bye' 'okay]