| by Arround The Web | No comments

How to Check if an Array Includes a Value in JavaScript

While coding with JavaScript, there are some situations in which programmers need to determine whether the element exists in the array or not. For this, JavaScript provides various pre-built methods, such as the includes() method or the indexOf() method, that help to check an array’s particular element.

This post will illustrate the different ways to verify whether an array includes a value in JavaScript.

How to Check if an Array Includes a Value in JavaScript?

To determine if an array includes a value in JavaScript, use the following methods:

Method 1: Check if an Array Includes a Value Using includes() Method

To check if an array includes a value, use the “includes()” method. If an array contains a particular element/value, the includes() method returns true. Moreover, it is a case-sensitive method.

Syntax
Follow the given syntax to verify the value included in an array or not:

array.includes(element)

Example
Create an array of prime numbers called “primeNumberArray”:

var primeNumberArray = [1, 2, 3, 5, 7, 9, 11, 13, 15];

Check whether “11” includes in an array using the “includes()” method:

primeNumberArray.includes(11);

The output displays “true”, which means “11” exists in an array:

Method 2: Check if an Array Includes a Value Using indexOf() Method

You can also use the “indexOf()” method to verify whether the specified value is included in an array or not. This method returns “-1” if an element cannot be found, else it returns the initial index in the array at which it can be located.

Syntax
Follow the below-given syntax to use the indexOf() method:

array.indexOf(element)

Example
Invoke the indexOf() method with value “11”, if the returned value is not equal to -1, it means the specified element exists in the array:

primeNumberArray.indexOf(11) !== -1;

Output

Method 3: Check if an Array Includes a Value Using some() Method

Another way is to utilize the “some()” method to determine the value included in an array. This method determines if the array contains at least one member that satisfies the test set by the given function. It returns “true” if it identifies an element in the array for which the specified function returns true, otherwise, it returns “false”.

Syntax
Use the given syntax for utilizing the some() method to check the value included in an array:

array.some((element) => { /* ... */ })

Example
Invoke the some() method to identify whether “11” includes in an array or not:

primeNumberArray.some(value => value === 11);

The output indicates that the specified element includes in an array:

We have compiled all the essential instructions related to verifying if an array includes a value in JavaScript.

Conclusion

To check if an array includes a value in JavaScript, use the “includes()” method, “indexOf()” method, or the “some()” method. All of these methods give “true” if the particular element/value is included in an array, else they return “false”. In this post, we illustrated the different ways to verify whether a specified value includes an array in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply