The JavaScript 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.
That’s where the JavaScript includes()
function comes in. JavaScript 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.
In this tutorial, we are going to discuss the basics of the JavaScript includes method and explore how you can use it to check whether an array contains a particular item.
JavaScript Array Refresher
An array in JavaScript is an ordered object that can be used to store data. JS arrays can include zero or more items and are useful when you want to store multiple values in a single variable. For example, you may have a list of order numbers that you want to store in one variable, rather than in multiple variables throughout your program.
Arrays in JavaScript are declared as a list of comma-separated values, enclosed within square brackets. Here’s an example of an array in JavaScript that stores a list of employee names:
employees = ['Jeff', 'Linda', 'Emma', 'Luke'];
The 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:
console.log(employees);
Our program returns:
['Jeff', 'Linda', 'Emma', 'Luke']
JavaScript Array Includes
The JavaScript array includes()
method can be used to determine whether an array contains a particular value. The method returns a true
or false
value depending on whether or not the array contains the value you have specified.
The array 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: true.
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 true
.
Similarly, the 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 false
.
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.
The second 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 0
in our array. So, our program returns false.
Conclusion
The JavaScript includes()
method can be used to check whether an array contains a particular value, and returns true
or 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.
In this tutorial, we explored the basics of arrays in JavaScript. Then, we went on to discuss the JavaScript 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.
Now you’re ready to start using the JavaScript 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.