

Then use indexOf() to return the first index of the string "rounded snout": alligatorFacts. Let’s revisit the sample array of alligator facts: const alligatorFacts =

If there is no match, the method returns -1. The indexOf() method returns the first index of a value in an array. Includes() is helpful for use cases where you only need to know if a value exists in an array. This code will return false because the string value '80' is not in the array. This code will return true because the numeric value 80 is in the array. includes() method uses strict comparison. Now, there are a few important things to notice. If you were to add a fromIndex parameter so the comparison occurs after "thick scales", it would return false: alligatorFacts. This code would return true, because the string exists in the array. Then use includes() to check if the string "thick scales" exists in the array: alligatorFacts.

Here is a sample array of alligator facts: const alligatorFacts = The default is 0, so the entire array is searched. The second parameter, fromIndex, is optional and sets the index from which to begin comparisons. The first parameter, valueToFind, is the value to match in the array. The includes() method returns either a true or a false if a value exists in an array or not. If you would like to follow along with this article, you will need: In this article, you will learn about Array.includes(), Array.indexOf, Array.find(), and Array.filter. Now there are several built-in utility methods that solve some of the common tasks for searching for values in an array. Prior to ECMAScript 6, you probably would have used a for loop to iterate through all the items in the array and perform operations on each item. Depending on the task at hand, you may be interested in a boolean value for confirmation, an index for the position of the value in the array, or a separate array containing all the search results. A common task will be searching the array to find if it contains a value (or values) that satisfies certain search criteria. In JavaScript, you will often be working with data that is stored in Arrays.
