Check if a value exists in an array using Javascript.
Murtaja Ziad
Posted on December 26, 2020
In this article, I will show you the 2 ways for checking if a value exists in an array.
1. Using includes()
This method returns true
if the array includes the value, else it returns false
.
let array = ["a", "b", "c", "d"];
console.log(array.includes("b")); // true
console.log(array.includes("e")); // false
2. Using indexOf()
This methods returns the index of the value if it exists, else it returns -1
.
let array = ["a", "b", "c", "d"];
console.log(array.indexOf("b")); // 1
console.log(array.indexOf("e")); // -1
💖 💪 🙅 🚩
Murtaja Ziad
Posted on December 26, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.