JavaScript Array includes()

The JavaScript Array includes() method checks if an array has a certain value among its entries.

The syntax of the includes() method is:

arr.includes(valueToFind, fromIndex)

Here, arr is an array.


includes() Parameters

The includes() method takes in:

  • valueToFind - The value to search for.
  • fromIndex (optional) - The position in the array at which to begin the search. By default, it is 0.

Note: For negative values, the search starts from array.length + fromIndex. (Counting from backward) For example, -1 represents the last element.


Return value from includes()

  • Returns true if valueToFind is found anywhere within array.
  • Returns false if valueToFind is not found anywhere within array.

Note: The includes() method is case sensitive for strings and character values.


Example: Using includes() method

let languages = ["JavaScript", "Java", "C", "C++", "Python", "Lua"];

let check = languages.includes("Java");
console.log(check); // true

// case sensitive
let check1 = languages.includes("java");
console.log(check1); // false

// second argument specifies position to start at
let check2 = languages.includes("Java", 2);
console.log(check2); // false

// negative argument starts count from backwards
// start searching from third-to-last element
let check3 = languages.includes("Java", -3);
console.log(check3); // false

let check4 = languages.includes("Ruby");
console.log(check4); // false

Output

true
false
false
false
false

Recommended Reading: JavaScript Array indexOf()