How to check if an item is in an array in JavaScript
If you have an array, and need to check if the array contains a certain value, how do you do this?
Use the includes
method:
["banana", "apples", "pears", "peaches"].includes("apples");
This will return true
since apples
appears in the array. Check out this CodeSandbox to have a play around:
This is part of the JavaScript bite-size series of posts. Check out the rest here!