JavaScript: Check if Array Includes a Value/Element

Introduction

Arrays are one of the most widely used data structures in Computer Science. While dealing with a list of items (array), we are often required to look for a particular value in the list. JavaScript contains a few built-in methods to check whether an array has a specific value, or object.

In this article, we'll take a look at how to check if an array includes/contains a value or element in JavaScript.

Check Array of Primitive Values Includes a Value

Array.includes() Function

The simplest way to check for a primitive value in an array is to use the includes() method:

let isInArray = arr.includes(valueToFind[, fromIndex])
// arr         - array we're inspecting
// valueToFind - value we're looking for
// fromIndex   - index from which the seach will start (defaults to 0 if left out)
// isInArray   - boolean value which tells us if arr contains valueToFind

For example, let's check whether the array of animals contains the dog and cat emojis:

let animals = ["🐘", "🐒", "🐶", "🐍"]

animals.includes("🐶") // true
animals.includes("🐱") // false

The function returns a boolean value, signifying the value's presence or lack thereof.

Array.indexOf() Function

In cases where we need the exact location of the element we're looking for, we can use the indexOf(elem) method, which looks for elem in the specified array and returns the index of its first occurrence, and -1 if the array does not contain elem.

For example, we can look for the first occurrence of a grade in an array containing grades:

let grades = ["B", "D", "C", "A"]

grades.indexOf("A") // 3
grades.indexOf("F") // -1

In the first instance, the element is present, and its position is returned. In the second instance, the return value signifies that the element is not present.

We can use this to alter code flow easily:

let grades = ["B", "D", "C", "A"]

if (grades.indexOf("F") >= 0) {
    console.log("Element is present");
} else {
    console.log("Element is not present");
}

If we pass in F, the adequate message is printed:

Element is not present

Checking if Array of Objects Includes Object

some() Function

When searching for an object, includes() checks whether the provided object reference matches the one in the array. This is rarely what we want, because objects can have identical fields with corresponding values but different references.

We can use the some() method to search by object's contents. The some() method takes one argument accepts a callback, which is executed once for each value in the array until it finds an element which meets the condition set by the callback function, and returns true.

Let's look at some() in action to understand it better:

let animals = [{name: "dog"}, {name: "snake"}, {name: "monkey"}, {name: "donkey"}]
let element = {name: "monkey"}

animals.some(animal => animal.name === element.name)

The callback function returns false for the first two cases, but returns true for the third element, as the names match. After this, some() halts execution and returns true.

Conclusion

In this article, we've gone over the few ways to check whether an array contains a value or not, in JavaScript.

We've covered the includes() function, which returns a boolean value if the value is present. The indexOf() function returns the index of a value if it's present, and -1 if it isn't.

Finally, for objects, the some() function helps us search for object presence based on their contents.

Author image
India Website
Hey, I am a full-stack web developer located in India. I am a curious person who is always trying to wrap my head around new technologies. In my free time, I read novels and play with my dog!