How to Check if an Array Includes a Value in JavaScript

Srijan } Author Pic
SrijanJan 18, 2025 - JavaScript
How to Check if an Array Includes a Value in JavaScript - Reacted Node

Photo: Andres Ayrton/Pexels

Introduction

In this article, we will learn about the includes() method in JavaScript, which helps us determine whether an array contains a specific value. It's like playing hide-and-seek with data—you have a value and need to check if it's hiding somewhere in the array.

Table of Contents

The includes() method

Let's consider a real-world example to understand the includes() method better. Imagine you are hosting a party. You have a guest list with the names of all the people you have invited. When someone arrives at the door, you must quickly check if their name is on the list. We will use the includes() method for the above example and write a simple solution for the above scenario.

1const guestList = ["Alice", "Bob", "Rahul", "John"];
2
3// When Bob is at the door
4console.log(guestList.includes("Bob")); // returns true
5
6// When David is at the door
7console.log(guestList.includes("David")); // returns false

When a person from the guestList arrives at the door, the includes() method returns true or otherwise false.

Syntax: includes() method

1array.includes(valueToFind, [fromIndex]);
  • valueToFind: The value you want to check for in the array.
  • fromIndex(optional): This is the index from where you start looking for the value in the array.

Starting search from a particular index

You might want to start checking from a specific point in the array, not always from the beginning. This is where fromIndex comes in handy. Consider you're only interested in the latter half of your language list:

1const languages = ["Python", "Ruby", "JavaScript", "C++"];
2const includesRuby = languages.includes("Ruby", 2);
3console.log(includesRuby); // Output: false

Case Sensitivity and Type Checking

Remember, includes() is case-sensitive and type-specific. It won't find ' JavaScript ' if you're looking for 'javascript'. Similarly, you'll get different results if you check for a number as a string (like '5') versus a number (like 5).

1const numbersArray = [5, 4, 3, 2, 1];
2numbersArray.includes("5"); // returns false

Some use-cases of includes() method

  • Form Validation: Ensure that user input matches a list of valid options.
  • Filtering Data: Quickly identify items in an array that meet certain criteria.
  • Checking Permissions: Verify if a user has a specific role or access level.
  • Search Functionality: Determine if a search term is present within a dataset.
1// Form Validation
2const validColors = ["red", "green", "blue"];
3const userInputColor = "purple";
4
5if (!validColors.includes(userInputColor)) {
6 alert("Invalid color choice!");
7}
8
9// Filtering Data
10const fruits = ["apple", "banana", "mango", "orange", "pineapple", "grape"];
11const tropicalFruits = ["mango", "banana", "pineapple"];
12
13const filteredFruits = fruits.filter((fruit) => tropicalFruits.includes(fruit));
14
15console.log(filteredFruits); // Output: ["banana", "mango", "pineapple"]
16
17// Checking Permissions
18const userRoles = ["admin", "editor"];
19
20if (userRoles.includes("admin")) {
21 // Allow access to admin features
22}