DEV Community

anjan-dutta
anjan-dutta

Posted on

1 1

Check if an array includes a value in Javascript

In this article, I am going to describe 4 methods

  • Using The indexOf() method
  • Check if array includes a value in Javascript
  • Check for NaN
  • Check for Undefined

Using The indexOf() method

Using indexOf(), we can determine if an element is present in an array or not. Similar to the includes() method the indexOf() method also works on arrays and, it takes one argument.

See the code below.


var students = ["john", "jane", "jim"];

console.log( students.indexOf("jane") > -1 );  
// true

Enter fullscreen mode Exit fullscreen mode

Even though the indexOf() method supports both old and new browsers, it has few drawbacks.

Read more ...

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

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

Okay