9
254

How to Check if Array Contains Value in JavaScript

Reading Time: 3 minutes

There are 5 ways to easily check if Array contains a Value in JavaScript where most of them are 1 liner code. Let’s explore the methods.

Methods to Check if Array Contains a Value in JavaScript

Here are the list of 5 methods where we can easily check if array contains a value. Since each of them have their own properties and features, we can choose which to use depending on our needs.

  • Array includes() Method
  • Array indexOf() Method
  • Array some() Method
  • Array filter() Method
  • Set has() Method

Let’s look into each details with illustration below.

1. Array includes() Method

includes() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Check if the array itself contain certain value
  • Allow specifying the index to start from
  • Return true if the value exist
  • Does not modify the array

Below shows the example where we send an array with and without the search value to includes() method.

let myArray = ['one', 'two', 'three'];
console.log(myArray.includes('four'));  // false

myArray = ['one', 'two', 'three', 'four'];
console.log(myArray.includes('four'));  // true

myArray = ['one', 'two', 'three', 'four'];
console.log(myArray.includes('one', 3));  // false

2. Array indexOf() Method

indexOf() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Check if the array itself contain certain value
  • Return the first index if the element is found
  • Does not modify the array

Below shows the example where we send an array with and without search value to indexOf() method. If the value is found, it return the first index. Thus, if it does not return -1, value is found.

let myArray = ['one', 'two', 'three'];
console.log(myArray.indexOf('four'));  // -1

myArray = ['one', 'two', 'three', 'four'];
console.log(myArray.indexOf('four'));  // 3

3. Array some() Method

some() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Allow specifying of test to execute on every element
  • Return true immediately for first value to pass the test
  • Does not modify the array

Below shows the example where we define and arrow function that check if string is value. Then, we pass this function as the test into some() method.

let myArray = ['one', 'two', 'three'];

const containValue = (element) => element === 'four';
console.log(myArray.some(containValue));  // false

myArray = ['one', 'two', 'three', 'four'];
console.log(myArray.some(containValue));  // true

4. Array filter() Method

filter() is part of original Array standard built-in object method in JavaScript.

Its features includes:

  • Allow specifying of test to execute on every element
  • Return an array of elements that pass the test
  • Does not modify the array

Below shows the example where we define an arrow function that check if string is value. Then, we pass this function as the test into filter() method. If there is no such value, we will receive empty array.

let myArray = ['one', 'two', 'three'];

const containValue = (element) => element === 'four';
console.log(myArray.filter(containValue));  // []

myArray = ['one', 'two', 'three', 'four'];
console.log(myArray.filter(containValue));  // ["four"]

5. Set has() Method

Set is a standard built-in object in JavaScript. By converting an Array into Set, we can use its has() method to easily determine if the value exist.

Its features includes:

  • Return true if the Set contain that element with that datatype
  • Does not modify the array

Below shows the example where we define and arrow function that check if string is value. Then, we pass this function as the test into filter() method. If there is no such value, we will receive empty array.

let myArray = ['one', 'two', 'three'];
let mySet = new Set(myArray);
console.log(mySet.has('four'));  // false

myArray = ['one', 'two', 'three', 'four'];
mySet = new Set(myArray);
console.log(mySet.has('four'));  // true

Conclusion

We have look into 5 different methods to check if there is a specify value in an array.

If we do not need to know about the position of the value, Array includes() and some() as they will just return a boolean result. Set has() method also returns a boolean and it maybe more efficient for processing huge arrays.

If we need to know about the position, array indexOf() will be useful since we can immediately perform processing.

Show Comments

No Responses Yet

Leave a Reply