JavaScript array.includes() function


The array.includes() method of JavaScript is used to check whether an array contains a specified element.

The syntax is as follows −

array.includes(ele, start)

Above, the parameter ele is the element to search for. The start parameter is the position to begin the search with.

Let us now implement the array.includes() method in JavaScript −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Car Variant</h2>
   <button onclick="display()">Result</button>
   <p id="test"></p>
   <script>
      var carid = ["110", "230", "299", "399"];
      document.getElementById("test").innerHTML = carid;
      function display() {
         var n = carid.includes("230");
         document.getElementById("test").innerHTML = "The array has the specific value 230? = "+n;
      }
   </script>
</body>
</html>

Output

Above, the click the “Result” button −

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
   <h2>Car Variant</h2>
   <p>Does the array includes the specific element Crossover?</p>
   <button onclick="display()">Result</button>
   <p id="test"></p>
   <script>
      var car = ["Hatchback", "Convertible"];
      var res2 = car.entries();
      for (val of res2) {
         document.getElementById("test").innerHTML += val + "<br>";
      }
      function display() {
         var res = car.includes("Crossover");
         document.getElementById("test").innerHTML = res;
      }
   </script>
</body>
</html>

Output

Click on the “Result” button above −

Updated on: 17-Dec-2019

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements