DEV Community

Murtaja Ziad
Murtaja Ziad

Posted on • Originally published at blog.murtajaziad.xyz on

5 1

Check if a value exists in an array using Javascript.

In this article, I will show you the 2 ways for checking if a value exists in an array.


Photo by Luis del Río from Pexels


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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay