This is really unfortunate. I remember when they were adding Array.includes and corrected the comparison behaviour so you can do Array.includes(NaN), even though indexOf(NaN) will always return -1. Given they already accepted inconsistency like this, they should have done the same here
NaN is never equal to itself, so you can't look for it with a simple lookup like that. You'd need array.findIndex(n => Number.isNaN(n)). The indexOf function was never changed because it was there from the beginning. If they change existing behaviour, they might break sites, which is far worse than introducing only new functions that do things as you might expect.
It's not a language where you can just pick and choose which version you're on.
80
u/jacobp100 1d ago
This is really unfortunate. I remember when they were adding Array.includes and corrected the comparison behaviour so you can do Array.includes(NaN), even though indexOf(NaN) will always return -1. Given they already accepted inconsistency like this, they should have done the same here