javascript includes code example

Example 1: javascript string contains

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

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

Example 2: javascript array contains

var colors = ["red", "blue", "green"];
var hasRed = colors.includes("red"); //true
var hasYellow = colors.includes("yellow"); //false

Example 3: javascript string includes

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

Example 4: string contains in javascript

const string = "foo";
const substring = "oo";

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

Example 5: javascript includes

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

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

Example 6: javascript includes

includes(searchElement)
includes(searchElement, fromIndex)

Tags: