Nikhil Nambiar
1 min readJan 30, 2020

--

How to compare a string to all the elements in an array?

The best way to compare all elements in an array is by using the includes() function.

Note: the includes function does not work with object comparison.

Here is a use case:

let blackFruits = ["black plum", "grapes", "blackberry"]blackFruits.push({id: 1, name: "blackberry"})// ["black plum", "grapes", "blackberry", {id: 1,  name:"blackberry"}]blackFruits.includes("grapes")// trueblackFruits.includes({id: 1, name: "blackberry"})// false

--

--