# How to check NaN?

Hi guys. Do the puzzle where you have to rewrite the method includes the built-in arrays.

Do this:
``````Array.prototype.newInclude = function(element, index) {
let startIndex = index || 0,
arr = this.slice(startIndex);

for (let i = 0; i < arr.length; i++) {
if (element === arr[i] || isNaN(arr[i]) && isNaN(element)) {
return true;
}
}
return false;
}``````

With numbers everything goes well, but the lines are not very.
The result:
``````['a','b'].newInclude('a') // true
['a','b'].newInclude('e') // true``````
that is , all the characters give true. I understand that this is using isNaN, but where would I tweak?
isNaN here because I wanted to achieve a result:
`[1,2,3,NaN].newInclude(NaN) // => true`

Thank you all!
June 10th 19 at 16:24
June 10th 19 at 16:26
Solution
Here is the corrected version https://jsfiddle.net/5zqfno0x/
It uses a feature of NaN, which is that
`NaN !== NaN // true`

Another option using the Number.isNaN instead of isNaN https://jsfiddle.net/gz2onxrh/
But Number.isNaN is a specification ES6. Feature is not supported in IE11 and other older browsers.

The problem of function isNaN is that it first converts the argument to a number and the string contains anything besides have cissel and spaces, objects, undefined turns into NaN
``````isNaN(undefined); // true
isNaN({}); // true
isNaN('test'); // true``````
Yeah, got it, thanks! - ludwig33 commented on June 10th 19 at 16:29
June 10th 19 at 16:28
Solution
Use here Number.isNaN instead of isNaN. What is the difference between them - can Google, useful in life.

Find more questions by tags JavaScript