JavaScript: How to check whether an array includes a particular value or not?

JavascriptWeb DevelopmentFront End Technology

In this article, we will be discussing the construction of an array. Once the array is created we will be looking out for a value by checking whether it exists in the array or not.

But before checking for any particular value, let’s see how the arrays are created in JavaScript.

Syntax

let array = [arrayItem1, arrayItem2, arrayItem3 ...]

Once the array is created, let’s see how to search for a specific value from the array. There are multiple approaches to searching for whether an element exists in the array or not. Below we are going to discuss the same.

Approach #1

This is a traditional approach to checking for a value in the array.

In this approach, we will loop over all the values of the array using the for loop and check the equality of each value with the value that needs to be searched.

Once the value is found we will break the loop and return true. Else we will return false if the iteration is completed without searching.

Example 1

In the below example, we are going to loop over the values to check if the desired value exists in the array or not.

# index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <title>ncode & Decode URL</title>
</head>
<body>
   <h1 style="color: green;">
      Welcome To Tutorials Point
   </h1>
   <script>
      let employees_array = [
         "steve",
         "mark",
         "mike",
         "bill",
         "henry",
         "orion",
      ];
      let valueChecker = (value) => {
         for (let i = 0; i < employees_array.length; i++) {
            let current_value = employees_array[i];
            if (value === current_value) {
               return value + " is present at index: " + i;
            }
         }
         return value + " is not included in this array..";
      };
      console.log(valueChecker("mike"));
      console.log(valueChecker("casey"));
      console.log(valueChecker("henry"));
   </script>
<body>
</html>

Output

The results can be find in the console −

Approach 2

Other than the traditional approach, we can also use the includes() method provided by JavaScript.

The includes() method checks for a particular value and returns true or false. True is returned when the value exists in the array, else false is returned.

Example 2

In the below example, we will use the includes() method to check if the value exists in the array or not.

# index.html

<!DOCTYPE html>
<html lang="en">
<head>
   <title>Encode & Decode URL</title>
</head>
<body>
   <h1 style="color: green;">
      Welcome To Tutorials Point
   </h1>
   <script>
      let employees_array = [
         "steve",
         "mark",
         "mike",
         "bill",
         "henry",
         "orion",
      ];
      console.log("Is Mark Exists: " +
      employees_array.includes("mark"));
      console.log("Is Orion Exists: " +
      employees_array.includes("orion"));
      console.log("Is Michael Exists: " +
      employees_array.includes("michael"))
   </script>
</body>
</html>

Output

Please see the console to find the results −

raja
Updated on 22-Apr-2022 13:31:28

Advertisements