JavaScript Array.includes(): Check if array contains specified value

Do you really want to know how to use the JavaScript array.includes() method to check if an array contains a specified value?

Read on to discover how to leverage the array includes() method in JavaScript to check if an array contains a specific value.

In this article, you’ll learn how to effectively use the array.includes() method in different scenarios and understands its case sensitivity.

What is JavaScript array includes()method?

The array.includes() method in JavaScript is a handy tool that helps you find out if a certain value exists in an array or if array contains the specified value.

It does this by going through each item in the array and comparing it to the value you’re looking for.

If it finds a match, the array.includes() method returns true; if not, it returns false.

Syntax

array.includes(searchElement[, fromIndex]) ✅

Parameters

searchElement (Required)

The value to search for in the array.

fromIndex (Optional)

The fromIndex parameter in the array determines the starting point for the search for the searchElement. If fromIndex is a positive value, the search starts at that index in the array.

If it’s negative, the search starts at arr.length + fromIndex, effectively counting back from the end of the array. If fromIndex is not provided, the search starts from the beginning of the array, which is index 0.

Return value

The array.includes() method returns a Boolean (true or false). It will return true if the array contains the specified value, and false if not.

Here’s an example:

let subjects = ["Math", "English", "Programming"];
console.log(subjects.includes("Programming"));✅  
console.log(subjects.includes("Software Engineering")); 

true

false

In our example code above, the first console log returns true because “Programming” is included in the subjects array.

The second console log returns false because “Software Engineering” is not included in the subjects array.

Moreover, the array.includes() method in JavaScript is case sensitive. It simply means that it treats lowercase and uppercase letters as distinct values.

For instance, if you have an array [“a”, “b”, “c”], the method includes(“A”) would return false because “A” (uppercase) is not the same as “a” (lowercase).

If you want to perform a case-insensitive check, you would need to convert both the array elements and the value you’re checking to the same case (either all to lowercase or all to uppercase) before calling includes().

Supported browser

✔ Chrome

✔ Edge

✔ Firefox

✔Opera

✔Safari

How to use JavaScript array.includes() to check if an array contains a specified value?

Here’s how you can use the Array.includes() method in JavaScript to check if an array contains a specified value:

let array = [10, 20, 30, 40, 50];

let valueToFind = 50;

if (array.includes(valueToFind)) ✅ {
console.log(`The array contains the value ${valueToFind}.`);
} else {
console.log(`The array does not contain the value ${valueToFind}.`);
}

As you can see, in our given example the array.includes() method is used to check if the array contains the valueToFind.

If the array contains the valueToFind, it prints “The array contains the value 50.” If it doesn’t, it prints “The array does not contain the value 50.”

Output:

The array contains the value 50.

Different ways on how to check if array contains specified value

As we mentioned earlier, you can use an array.includes() method to check if an array contains a specified value in JavaScript.

Aside from array.includes, here are a few different ways which you may also use:

Using the Array.indexOf() method

Here’s an example:

let array = [100, 200, 300, 400, 500];
let valueToFind = 100;

if (array.indexOf(valueToFind) !== -1) ✅  {
console.log(`The array contains the value ${valueToFind}.`);
} else {
console.log(`The array does not contain the value ${valueToFind}.`);
}

As observed, array.indexOf() yields the initial index where a specific element is located within the array. If the element is not present, it returns -1.

Output:

The array contains the value 100.

Using the Array.find() method

Here’s an example:

let array = [100, 200, 300, 400, 500];
let valueToFind = 200;

if (array.find(element => element === valueToFind))✅  {
console.log(`The array contains the value ${valueToFind}.`);
} else {
console.log(`The array does not contain the value ${valueToFind}.`);
}

In this method, Array.find() returns the first element in the array that satisfies the provided testing function. Otherwise undefined is returned.

Output:

The array contains the value 200.

Conclusion

In conclusion, the array.includes() method in JavaScript is used to easily check if an array contains a specific value.

This method is case-sensitive and returns a boolean value, making it highly versatile for various scenarios.

In addition to array.includes(), we’ve looked at alternative methods like array.indexOf() and array.find(), expanding the ways you can interact with arrays in JavaScript.

We are hoping that this article provides you with enough information that helps you understand the JavaScript array includes.

If you want to dive into more JavaScript topics, check out the following articles:

Thank you for reading Itsourcecoders 😊.

Leave a Comment