Check If An Array Includes A Value In JavaScript

You can check if an array includes a value in JavaScript using any of these methods –

  1. Use Array.includes() method
  2. Use Array.indexOf() method
  3. Use Array.some() method
  4. Use for loop
  5. Use underscore.js
  6. Use Lodash library
  7. Use jQuery

Let’s look at each of these methods in detail below –

check if an array includes a value in javascript

1) Array.includes() Method To Check If An Array Includes A Value

The includes() method is used to check if an array includes a value in JavaScript.

This method returns a Boolean value – true if the element is present in the array, and false if it is not present.

Syntax:

array.includes(element, start)

Parameter:

element: The element to be searched in the array.

start: Optional. The position in this array at which to begin searching for the given element. Default is 0.

Example:

var arr = ['a', 'b', 'c'];

console.log(arr.includes('a')); // true

console.log(arr.includes('d')); // false

2) Array.indexOf() Method

The indexOf() method is used to find the first occurrence of a specified value in an array. This method returns the index of the first occurrence of the given element, or -1 if it is not present in the array.

Syntax:

array.indexOf(element, start)

Parameter:

element: The element to be searched in the array.

start: Optional. The position in this array at which to begin searching for the given element. Default is 0.

Example:

var arr = ['a', 'b', 'c'];

console.log(arr.indexOf('a')); // 0

console.log(arr.indexOf('d')); // -1

3) Array.some() Method

The some() method is used to check if at least one element in an array passes the test implemented by a given function.

This method returns a Boolean value – true if at least one element passes the test, and false if all elements fail the test.

Syntax:

array.some(function(currentValue, index, arr), thisValue)

Parameter:

function(currentValue, index, arr): This function is invoked for each element of the array until it returns a truthy value. It is invoked with three arguments –

currentValue: The current element being processed in the array.

index(Optional): The index of the current element being processed in the array.

arr(Optional): The array that some() was called upon.

thisValue(Optional): A value to be passed as the this parameter to the function being invoked.

Example:

var arr = ['a', 'b', 'c'];

console.log(arr.some(function(element) {

return element === 'a';

})); // true

4) For Loop

You can also use a for loop to check if an array contains a given value.

In the example below, we have an array of integers and we will check if it includes the number 9.

Example:

var arr = [1,4,5,6,8,9];

var flag = false;

for(i = 0; i < arr.length; i++) {

if(arr[i] === 9) {

flag = true;

break;

}

}

console.log(flag); // true

5) Underscore.js

Underscore is a JavaScript library that provides a lot of utility functions for working with arrays, numbers, objects, strings, etc.

It also has a function called _.contains() that can be used to check if an array includes a given value.

Example:

var arr = ['a', 'b', 'c'];

console.log(_.contains(arr, 'a')); // true

console.log(_.contains(arr, 'd')); // false

6) Lodash Library

Lodash is a JavaScript library that provides utility functions for working with arrays, objects, numbers, strings, etc.

It has a function called lodash.includes() that can be used to check if an array includes a given value.

Example:

var arr = ['a', 'b', 'c'];

console.log(lodash.includes(arr, 'a')); // true

console.log(lodash.includes(arr, 'd')); // false

7) jQuery

jQuery is a JavaScript library that provides a lot of utility functions for working with DOM elements, events, AJAX, etc.

It also has a function called $.inArray() that can be used to check if an array includes a given value.

Syntax:

$.inArray(value, array)

Parameter:

value: The value to be searched for in the array.

array: The array in which to search for the given value.

Example:

var arr = ['a', 'b', 'c'];

console.log($.inArray('a', arr)); // 0

console.log($.inArray('d', arr)); // -1

Conclusion – Check If An Array Includes A Value In JavaScript

There are many ways to check if an array includes a value in JavaScript. Some of the most popular methods are using the indexOf() method, the some() method, or a for loop. You can also use the Underscore.js or Lodash libraries, or the jQuery $.inArray() function.

Which method you choose to use will depend on your specific needs. If you need to support older browsers, then you might want to stick with indexOf() or a for loop. But if you’re using a modern browser and don’t need to worry about compatibility, then some() or lodash.includes() might be a better choice.

Whichever method you choose, just be sure to test it thoroughly before using it in your production code. And if you’re ever in doubt, consult the documentation for the specific method or library you’re using.

Leave a Reply