Others



JavaScript Array includes()


The array includes() method in JavaScript is used to check whether a given value exists in an array. It returns true if the value is found in the array, and false otherwise.

Syntax
array.includes(valueToFind,startIndex)
  • valueToFind - The value to search for in the array.
  • startIndex - The index to start the search. Defaults to 0. If negative, it starts from the end of the array.
Example
const fruits = ["apple", "banana", "cherry"];

fruits.includes("banana"); //returns true
fruits.includes("grape"); //returns false
Try it Yourself

Return Value

  • true - if an array contains a specified value.
  • false - if the value is not found.

The includes() method is case sensitive.

Example
const fruits = ["apple", "banana", "cherry"];

fruits.includes("Banana"); //returns false
Try it Yourself

The includes() method is particularly useful for determining if a value exists in an array without having to write custom logic like using a for loop or indexOf.

Using FromIndex
const numbers = [1, 2, 3, 4, 5];

numbers.includes(3, 2); //returns true
numbers.includes(1, 2); //returns false
numbers.includes(3, -3); //returns true
Try it Yourself
Works with Different Types
const mixedArray = [1, "apple", true, null];

mixedArray.includes(1); //returns true
mixedArray.includes("apple");  //returns true
mixedArray.includes(false);  //returns false
Try it Yourself