Search code examples
javascriptarrayspolyfills

Javascript typeof and IsNaN


In MDN polyfill function for Array.prototype.includes(), I found the following code.

function sameValueZero(x, y) {
    return x === y || (typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y));
}

In the above code typeof x === 'number' && typeof y === 'number' && isNaN(x) && isNaN(y) this makes me confusing. If the typeof the variable is a number , isNaN will be always false right? so when this whole condition will return true? Can someone explain or did I understand it wrongly?


Solution

  • In JavaScript, the type of NaN is number. I guess this is because it is the result of mathematical operations.

    See this question for more information.