DEV Community


Posted on

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)