loading...

Includes() vs indexOf() in JavaScript

John Samuel Obinna on September 04, 2017

ES2016 Specifications included the includes() method for Array data structure. The includes() method check if an array includes a certain ... [Read Full]
markdown guide
 

Nice article.
I have one correction for you.

The line:
if(array.indexOf(4) >= -1 )

Needs to be:
if(array.indexOf(4) > -1 )

In order to check, whether an element is in the array. Otherwise the condition would always be true.

 

Yeah. indexOf() always return -1 if element is not found. Thanks Jonas

 

I'm wondering about performance. Meaning if includes or indexOf keep searching the array even though already found the element. MDN docs doesn't say if includes breaks the search once finds an element.
developer.mozilla.org/en-US/docs/W...

 

You forgot to mention one of the main differences between includes() and indexOf().

IE does not support includes() whereas it supports the other.

 

I think IE11 started supporting Array.prototype.includes. Exact version that I'm using is 11.904.16299.0

 
 

Uhm in all honesty isn't +0 exactly the same as -0? So is there any real life need to distinguish between +0 and -0?

 
code of conduct - report abuse