


true How to Check if a Substring is in a String in JavaScript Using the includes() Method If you change the index to start the search from to 1 then you'd get true returned because 3 can be found within that range. This is why searching for 3 from index 2 returned false.

So starting from the second index which is 5, we have only 5 and 7 () to be searched through. Using the second parameter, we told the includes() method to search for the number 3 but starting from index 2: nums.includes(3,2).

The example above returned false even though we had 3 as an item in the array. Here's an example to show how we can use the includes() method's second parameter: const nums = So the first item is 0, the second item is 1, the third item is 2, and so on. In the last section, we saw how to check if an item existed in an array without using the second parameter in the includes() method.Īs a reminder, the second parameter is used to specify the index to start from when searching for an item in an array. How to Check if an Item is in an Array in JavaScript Using Array.includes() Starting From a Specified Index const nums = Īs expected, we got false returned in the example above because 8 is not an item in the nums array. Let's try searching for a number that doesn't exist in the array. We got true returned because 3 exists in the nums array. In the includes() method's parameter, we passed in 3. Using dot notation, we attached the includes() method to the nums array. In the example above, we created an array called nums with four numbers – 1, 3, 5, 7. Here are some examples to show how to use the includes() method to check if an item exists in an array: const nums = If you don't include this parameter, the default index will be set to 0 (the first index).
