DEV Community

Avnish Jayaswal
Avnish Jayaswal

Posted on Edited on Originally published at askavy.com

How do I check if an array includes a value in JavaScript

The includes() method determines whether a javascript array contains a specified element.

This method returns true if the array contains the element, and false if element not exist in array.


var list = ["A", "B", "C", "D", "E"];

console.log(list.includes("A")); // true

 console.log(list.includes("A",1)); //returns false, as "A" is not present at index 1. 

console.log(list.includes("B",1)); //returns True , as "B" is present at index 1. 

console.log(list.indexOf("A") !== -1 ); // true

⚽ Live Match: askavy/how do i check if an array includes a value in javascript i - Info Live Streaming 2026 ⚽ Live Match: askavy/how do i check if an array includes a value in javascript i - Info Live Streaming 2026

Array indexOf function

how to check exact position of element using indexOf JS method

indexOf(element) method, which search for element in the specified array and returns the index of its first occurrence, and -1 if the array does not contain element


var JSArr = ["A", "B", "C", "D"];

console.log(JSArr.indexOf("A")) // 0  found at postion 0 
console.log(JSArr.indexOf("B")) // 1  found at postion 1 
console.log(JSArr.indexOf("F")) // -1  not found

⚽ Live Match: askavy/how do i check if an array includes a value in javascript i - Info Live Streaming 2026 ⚽ Live Match: askavy/how do i check if an array includes a value in javascript i - Info Live Streaming 2026

Example 2 using function


      var JSArr = ["A", "B", "C", "D"];

      if (JSArr.indexOf("A") !== -1) {
        alert("Value exists");
      } else {
        alert("Value does not exists");
      }

⚽ Live Match: askavy/how do i check if an array includes a value in javascript i - Info Live Streaming 2026 ⚽ Live Match: askavy/how do i check if an array includes a value in javascript i - Info Live Streaming 2026

Top comments (0)