Course

array.includes()

In this lesson, you'll learn about the method in JavaScript, which is used to determine whether an array contains a specific value.

Array.includes()

The method checks if an array contains a certain value and returns or based on the result. This method is case-sensitive, meaning it distinguishes between uppercase and lowercase letters.

Example: Checking for a Book in a Bookshelf

Let's say we're a librarian looking for a particular book inside a "bookshelf" array:

var bookshelf = ["Moby Dick", "Little Women", "The Great Gatsby", "Pride And Prejudice"];

Here's how you can use the method to check for a specific book:

if (bookshelf.includes("Moby Dick")) {
    console.log("The book you were looking for was found.");
} else {
    console.log("Couldn't find the book, sorry. :c");
}

How It Works

  • The method scrolls through the entire array and returns once it finds the first instance of the value it received.
  • If the value is not found , it returns .

The method is a straightforward and efficient way to check for the presence of a value in an array , making it a useful tool for data validation and search operations in JavaScript.

Loading...

0 Comments

"Please login to view comments"

glass-bbok

Join the Conversation!

Subscribing gives you access to the comments so you can share your ideas, ask questions, and connect with others.

Upgrade your account
tick-guideNext Lesson

Array.sort()