'JavaScript return true if this word exist in the data [duplicate]
in this case it return true because banana in the 0 index
const data= ["Banana", "Orange", "Apple", "Mango"];
if (data.indexOf("Banana")===0)
but if banana in another index i need it returns true agian how?
const data= [ "Orange","Banana", "Apple", "Mango"];
Solution 1:[1]
Use includes() function
if (data.includes("Banana"))
Solution 2:[2]
A few methods for achieving this:
- Using
array.indexOf:
if (data.indexOf("Banana") !== -1)
- Using
array.includes:
if (data.includes("Banana"))
- Using
array.some:
if (data.some(d => d === "Banana"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
