While browsing some Javascript
blogs, I stumbled upon the Array prototype methods .indexOf and .includes
. I noticed that if an array includes NaN
as a value, indexOf
may not be able to detect it, leaving us with the option of using .includes
. However, considering that includes
is not supported by IE, what can be used as an alternative method to check for NaN? I considered creating a polyfill using the guidance provided here
if (Array.prototype.includes) {
Object.defineProperty(Array.prototype, "includes", {
enumerable: false,
value: function(obj) {
var newArr = this.filter(function(el) {
return el == obj;
});
return newArr.length > 0;
}
});
}
var arr = [NaN];
console.log(arr.includes(NaN));
However, the output is still false. Are there any other alternatives available? Am I overlooking something?