Examples const fruits = ["Banana", "Orange", "Apple", "Mango"]; Show
Try it Yourself » Start the search at position 3: const fruits = ["Banana", "Orange", "Apple", "Mango"]; Try it Yourself » Definition and UsageThe The The Syntaxarray.includes(element, start) Parameters
Return Value
Browser Support
ES7 (JavaScript 2016) is supported in all modern browsers:
I benchmarked it multiple times on Google Chrome 52, but feel free to copypaste it into any other browser's console. ~ 1500 ms, includes (~ 2700 ms when I used the polyfill)
~ 1050 ms, indexOf
~ 650 ms, custom function You can use the It returns In this article, you'll see how to use the Here's the syntax for using the
Let's break down the syntax above:
The
Here are some examples to show how to use the
In the example above, we created an array called Using dot notation, we attached the In the We got Let's try searching for a number that doesn't exist in the array.
As expected, we got How to Check if an Item is in an Array in JavaScript Using Array.includes() Starting From a Specified IndexIn the last section, we saw how to check if an item existed in an array without using the second
parameter in the As a reminder, the second parameter is used to specify the index to start from when searching for an item in an array. The index of an array starts from 0. So the first item is 0, the second item is 1, the third item is 2, and so on. Here's an example to show how we can use the
The example above returned Using the second
parameter, we told the This is the array: [ 1, 3, 5, 7] Index 0 = 1. Index 1 = 3. Index 2 = 5. Index 3 = 7. So starting from the second index which is 5, we have only 5 and 7 ([5,7]) to be searched through. This is why searching for 3 from index 2 returned If you change the index to start the search from to 1 then you'd get
How to Check if a Substring is in a String in JavaScript Using the includes() MethodSimilar to the previous examples, you have to attach the Here's what the syntax looks like:
Here's an example:
In the example above, the Using the We got You can also use the second parameter to specify where the search will begin, but note that each character in a string represents an index and the spaces between each substring also represents an index. Here is an example to demonstrate that:
We are getting Starting from index 9, the string would look like this: "eb developer". The substring "web" doesn't exist in the string so SummaryIn this article, we talked about the We saw some examples that explained its use to check for an item in an array starting from the first index, then another example from a specified index. Lastly, we saw how to use the Happy coding! Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started How do you check if an item is inside an array JavaScript?You can use the includes() method in JavaScript to check if an item exists in an array. You can also use it to check if a substring exists within a string. It returns true if the item is found in the array/string and false if the item doesn't exist.
How do you check if an item exists in an array?The simplest and fastest way to check if an item is present in an array is by using the Array. indexOf() method. This method searches the array for the given item and returns its index. If no item is found, it returns -1.
How do you check if a value is not in an array JavaScript?To check if a value is not in array array, use the indexOf() method, e.g. arr. indexOf(myVar) === -1 . If the indexOf method returns -1 , then the value is not contained in the array.
How do you check if an array contains a string?To check if a string is contained in an array, call the indexOf method, passing it the string as a parameter. The indexOf method returns the index of the first occurrence of the string in the array, or -1 if the string is not contained in the array.
|