176

Beyond the improved readability, is there any advantage to includes over indexOf? They seem identical to me.

What is the difference between this

var x = [1,2,3].indexOf(1) > -1; //true

And this?

var y = [1,2,3].includes(1); //true
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Matt
  • 4,462
  • 5
  • 25
  • 35
  • 19
    `includes` has much worse browser support. – Quentin Feb 12 '16 at 19:09
  • 6
    Note that `includes` is not part of ES6/ES2015. It is a proposal for the next version of ECMAScript and will be added this year. – Felix Kling Feb 12 '16 at 19:13
  • 6
    just wanted to also mention that `includes` is NOT supported in IE at all – ZvKa Nov 09 '17 at 18:30
  • `includes` is around 50 times slower than indexOf, at least in Chrome. Beware! – Seven Systems May 08 '21 at 13:37
  • 1
    @SevenSystems do you have anything to demonstrate this? – M - Jun 16 '21 at 22:52
  • 1
    @Marquizzo yes, a simple loop that did a large amount of `foo.indexOf(bar)>=0` vs `foo.includes(bar)` was enough to demonstrate the issue. However, I repeated the test in a recent version of Chrome and the performance is now almost identical. – Seven Systems Jun 17 '21 at 23:19

11 Answers11

196

tl;dr: NaN is treated differently:

  • [NaN].indexOf(NaN) > -1 is false
  • [NaN].includes(NaN) is true

From the proposal:

Motivation

When using ECMAScript arrays, it is commonly desired to determine if the array includes an element. The prevailing pattern for this is

if (arr.indexOf(el) !== -1) {
    ...
}

with various other possibilities, e.g. arr.indexOf(el) >= 0, or even ~arr.indexOf(el).

These patterns exhibit two problems:

  • They fail to "say what you mean": instead of asking about whether the array includes an element, you ask what the index of the first occurrence of that element in the array is, and then compare it or bit-twiddle it, to determine the answer to your actual question.
  • They fail for NaN, as indexOf uses Strict Equality Comparison and thus [NaN].indexOf(NaN) === -1.

Proposed Solution

We propose the addition of an Array.prototype.includes method, such that the above patterns can be rewritten as

if (arr.includes(el)) {
    ...
}

This has almost the same semantics as the above, except that it uses the SameValueZero comparison algorithm instead of Strict Equality Comparison, thus making [NaN].includes(NaN) true.

Thus, this proposal solves both problems seen in existing code.

We additionally add a fromIndex parameter, similar to Array.prototype.indexOf and String.prototype.includes, for consistency.


Further information:

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 3
    Only half the truth. Also different: missing elements are treated as `undefined` by `.includes()` and are not considered by `.indexOf()`. – Robert Siemer Mar 03 '20 at 02:59
43

Technically

NaN will not be findable when using indexOf

[NaN].indexOf(NaN) // => -1 (not found)
[NaN].includes(NaN) // => true

includes also is of no use if you want to know at where index the element was found.

Readability

arr.indexOf('searchedElement') !== -1 is less more readable and maintainable.
arr.includes('searchedElement') on the other hand does what it says and it is obvious that it returns a boolean.

Performances

According to this article on the subject there are no noticeable difference although includes may be a very little bit slower.

History

indexOf was created way before includes.

TOPKAT
  • 6,667
  • 2
  • 44
  • 72
  • 1
    Seems like the difference in performance is not that obvious. My mobile Firefox shows that indexOf is actually faster. Some Chrome versions acts the same... But the difference seems negligible anyway in today's implementations. – Nux Feb 25 '18 at 11:03
  • Just opened a console and tested it: `let allElements = [...document.getElementsByTagName('*')]; let t1 = performance.now(); for(let i=0;i<1e7;i++){ randEl = allElements[Math.floor(Math.random()*allElements.length)]; const isIn = allElements.includes(randEl); } let t2 = performance.now(); for(let i=0;i<1e7;i++){ randEl = allElements[Math.floor(Math.random()*allElements.length)]; const isIn = (allElements.indexOf(randEl)>-1); } let t3 = performance.now(); console.log('Include time:', t2-t1); console.log('indexOd time:', t3-t2);` Result: includes 40 times faster. – Shimon S Oct 01 '20 at 14:24
  • I think the perfomance between the two depends on a lot of "variables", so it's not worth to worry that much about it with today's speed we have in our devices. Unless if you want to do a very specific solution, then you should worry about the speed based on the setup you have and pick the best solution for that specific setup /device/browser. – Frederiko Ribeiro Aug 14 '23 at 14:07
13

.indexOf() and .includes() methods can be used to search for an element in an array or to search for a character/substring in a given string.

Usage in Array

(Link to ECMAScript Specification)

  1. indexOf uses Strict Equality Comparison whereas includes uses the SameValueZero algorithm. Because of this reason, the following two points of differences arise.

  2. As pointed out by Felix Kling, the behavior is different in case of NaN.

let arr = [NaN];

arr.indexOf(NaN); // returns -1; meaning NaN is not present
arr.includes(NaN); // returns true
  1. The behavior is also different in case of undefined.
let arr = [ , , ];

arr.indexOf(undefined); // returns -1; meaning undefined is not present
arr.includes(undefined); // returns true

Usage in String

(Link to ECMAScript Specification)

1. If you pass a RegExp to indexOf, it will treat the RegExp as a string and will return the index of the string, if found. However, if you pass a RegExp to includes, it will throw an exception.

let str = "javascript";

str.indexOf(/\w/); // returns -1 even though the elements match the regex because /\w/ is treated as string
str.includes(/\w/); // throws TypeError: First argument to String.prototype.includes must not be a regular expression

Performance

As GLAND_PROPRE pointed out, includes may be a little (very tiny) bit slower (for it needs to check for a regex as the first argument) than indexOf but in reality, this doesn't make much difference and is negligible.

History

String.prototype.includes() was introduced in ECMAScript 2015 whereas Array.prototype.includes() was introduced in ECMAScript 2016. With regards to browser support, use them wisely.

String.prototype.indexOf() and Array.prototype.indexOf() are present in ES5 edition of ECMAScript and hence supported by all browsers.

Srishti Gupta
  • 1,155
  • 1
  • 13
  • 30
  • 1
    `indexOf` gives `true` after explicitly setting `undefined` into an array: `let arr=[undefined];` or `let arr=[];arr[0]=undefined;` Now `arr.indexOf(undefined) === 0` – Jay Dadhania Dec 28 '19 at 11:07
  • You are the only one indicating the difference with `undefined`: The reason is that `.includes` treats missing elements as `undefined`, while `.indexOf()` ignores them. You can also “create” missing elements with `arr[number beyond length] = whatever`. – Robert Siemer Mar 03 '20 at 03:09
  • 1
    No one was asking for strings, but your remarks suggest that `.includes()` only eats strings. In reality both methods coerce anything to a string as good as they can. Regex is specifically excluded as argument to `.includes()` to _allow for future additions to the standard_ according to the current draft. – Robert Siemer Mar 03 '20 at 03:17
8

Conceptually you should use indexOf when you want to use the position indexOf just give you to extract the value or operate over the array, i.e using slice, shift or split after you got the position of the element. On the other hand, Use Array.includes only to know if the value is inside the array and not the position because you don't care about it.

Sebastian Gomez
  • 684
  • 7
  • 10
  • Use indexOf if you want to support old browsers which is usually a good idea. Use includes if your code is not going to be executed in a browser and therefore you know that you don't need to support old browsers like IE which I guarantee you people still use. Don't use either if you need to test for values like NaN or undefined and still want to support old browsers. Just use an old fashioned for loop instead. There's no shame in writing a for loop and supporting IE for people who don't know that IE isn't a modern browser or don't have any choice because they're on a public/work computer. – PHP Guru Mar 04 '21 at 15:38
3

indexOf() and includes() can both be used to find elements in an array, however each function yields different return values.

indexOf returns a number (-1 if element not present in the array, or array position if the element is present).

includes() returns a boolean value (true or false).

segFault
  • 3,887
  • 1
  • 19
  • 31
Kruti
  • 159
  • 1
  • 4
  • 1
    Hi Kruti, this question has been already answered and it has an accepted answer. Furthermore, you can see how your answer contains only info already said in other answers. The question, in the end, is already 4 years old. Please provide guidance on newest questions or unanswered ones. Thank you – leonardfactory Apr 26 '20 at 10:46
1

Internet explorer does not support includes, if that helps you decide.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes#Browser_compatibility

1

The answers and examples were all great. However (at first glance), it made me misunderstood that includes will always return true when using undefined.

Hence I am including the example to elaborate includes can be used to check for undefined and NaN values wherelse indexOf can't

//Array without undefined values and without NaN values. 
//includes will return false because there are no NaN and undefined values

const myarray = [1,2,3,4]

console.log(myarray.includes(undefined)) //returns false
console.log(myarray.includes(NaN)) //returns false


//Array with undefined values and Nan values.
//includes will find them and return true

const myarray2 = [1,NaN, ,4]

console.log(myarray2.includes(undefined)) //returns true
console.log(myarray2.includes(NaN)) //returns true


console.log(myarray2.indexOf(undefined) > -1) //returns false
console.log(myarray2.indexOf(NaN) > -1) //returns false

Summary

  • includes can be used to check for undefined and NaN values in array
  • indexOf cannot be used to check for undefined and NaN values in array
Someone Special
  • 12,479
  • 7
  • 45
  • 76
0

indexOf is the older way to check if something is in the array , the new method is better because you don't have to write a condition for being (-1) , so that's why for use the include() method that returns you a boolean.

array.indexOf('something')      // return index or -1
array.includes('something')     // return true of false

so for finding index the first one is better but for checking being or not the second method is more useful.

  • if(array.includes('something')) { //doSomething } if(array.indexOf('something') > -1) { //doSomething } - diff between these two is > -1, const incls = array.includes('something') || const inOf = array.indexOf('something') > -1; which returns true or false. not much difference – Lakshman Kambam Apr 21 '21 at 08:33
0

includes is using automatic type conversion i.e. between string and number. indexOf doesn't.

0
 
let allGroceries = ['tomato', 'baked bean',];
 
//returns true or false
console.log(allGroceries.includes("tomato")); //uses boolean value to check 

let fruits2 = ["apple", "banana", "orange"];
console.log(fruits2.includes("banana"));
// returns true because banana is in the array



//.indexOf returns the index of the value

console.log(allGroceries.indexOf("tomato"));//returns the index of the value
//returns -1 because tomato is not in the array
//fromIndex
console.log(allGroceries.indexOf("tomato", 2));


0

The includes() method is slightly different than the indexOf() method in one important way. indexOf() tests equality using the same algorithm that the === operator does, and that equality algorithm considers the not-a-number value to be different from every other value, including itself. includes() uses a slightly different version of equality that does consider NaN to be equal to itself. This means that indexOf() will not detect the NaN value in an array, but includes() will:

let a = [1,true,3,NaN];
a.includes(NaN) // => true
a.indexOf(NaN) // => -1; indexOf can't find NaN

From JavaScript: The Definitive Guide by David Flanagan

Nasim B. D
  • 161
  • 1
  • 6