includes() method determines whether a list includes a specified item. The
includes() method takes one parameter, the value to check for in a list.
includes() will return true if that value is present, and false if it’s not.
Often, when you have a list of items, you want to check whether or not that list contains a particular element. For example, you may have a list of pending order numbers and want to check whether or not order #500 is pending, or has already been processed.
includes() is a built-in function that can be used to determine whether an array contains a particular item. The function returns a true or false value depending on whether the array contains the item you have specified.
- Career Karma matches you with top tech bootcamps
- Get exclusive scholarships and prep courses
employees = ['Jeff', 'Linda', 'Emma', 'Luke'];
employees array we have just created contains four values: Jeff, Linda, Emma, and Luke. By using the
console.log() function, we can see the contents of our array:
Our program returns:
['Jeff', 'Linda', 'Emma', 'Luke']
includes() method can be used to determine whether an array contains a particular value. The method returns a
false value depending on whether or not the array contains the value you have specified.
includes() method takes two parameters. The first one is the value you are searching for in an array, and the second is the index position in the array at which the search should start. When you’re using the
includes() method, remember the case sensitivity of the method. If your value parameter does not use the right case, the value you are looking for will not be returned.
Let’s say that we have an ice cream store and we want to figure out whether anyone has ordered our
Double Chocolate Deluxe ice cream today. We have an array of data which contains all the orders that have been placed today, and we can use the
includes() method to check if that array contains the value
Double Chocolate Deluxe.
Here’s the code we would use to perform this action:
var orders_today = ['Strawberry', 'Chocolate', 'Chocolate', 'Raspberry', 'Vanilla', 'Vanilla', 'Double Chocolate Deluxe']; console.log(orders_today.includes('Double Chocolate Deluxe'));
Our code returns the following:
Let’s break down our code. On the first line, we declare an array called
orders_today which stores all the orders that were placed today. On the next line, we use the
includes() method to determine whether or not our
orders_today array includes the value
Double Chocolate Deluxe. Because our array does include that value, the program returns
includes() method can be used to find a number in an array of numbers. Let’s say that we have an array which stores a list of pending order numbers in our ice cream store, and we want to check if order number 27 is still pending. We could use the following code to perform this action:
var pending_order_numbers = [24, 26, 28, 29]; console.log(pending_order_numbers.includes(27));
Our code returns: false. The
includes() method has checked if
pending_order_numbers includes the value
27. Because it does not, our program returns the value
Search From a Specific Position
In the above examples, we have only specified the value we are searching for in our array. But what if we only want to search for a specific value after a certain position? That’s where the second, optional,
includes() parameter comes in.
includes() parameter is used to specify the index value at which our search should begin. Let’s say that we want to search our array of orders to see if a
Strawberry ice cream was ordered today. However, we want to ignore the first order because that was a test ice cream order. We could use the following code to perform this search:
var orders_today = ['Strawberry', 'Chocolate', 'Chocolate', 'Raspberry', 'Vanilla', 'Vanilla', 'Double Chocolate Deluxe']; console.log(orders_today.includes('Strawberry', 1);
Our program returns: false. Even though
orders_today includes an order for strawberry ice cream, it appears at the position
in our array. So, our program returns
includes() method can be used to check whether an array contains a particular value, and returns
false depending on whether that value is found.
includes() is particularly useful if you want to check if a value exists in an array with many items.
includes() method and explored how to use it with arrays of numbers and string. We also discussed how to use the optional parameter to specify where the
includes() function should start its search.
includes() method like an expert!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.