This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis
That's a really quick one. I was sitting in Jeff Strauss' talk at KCDC on ES2017 and he mentioned a tiny detail about Array.prototype.includes
I didn't think of before.
Let's say you have an array with several different types like ['foo', 123, true, undefined, NaN]
. You could now figure out if these values are included in the array by using indexOf
and checking if it returns -1
, right? Well... :D
This works fine for all the values except NaN
because NaN === NaN
evaluates to false
.
['foo', 123, true, undefined, NaN].indexOf(NaN) // -1
Array.prototype.includes
fixes this behavior. ?
['foo', 123, true, undefined, NaN].includes(NaN) // true
Reply to Stefan
This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Stefan Judis | Sciencx (2017-08-08T05:00:00+00:00) includes really is the better indexOf (#tilPost). Retrieved from https://www.scien.cx/2017/08/08/includes-really-is-the-better-indexof-tilpost/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.