Home > Enterprise >  How to understand isNaN
How to understand isNaN

Time:06-18

I try below code block, the isNaN return true. The value is not NaN why the return value is true?

true 06-19 12:20:30.200

        var t = d3.timeParse("%Y-%m-%d %H:%M:%S.%L")("2022-06-19 12:20:30.200");
        v = d3.timeFormat("%m-%d %H:%M:%S.%L")(t)
        var isNan = isNaN(v)
        console.log(isNan,v)
<script src="https://unpkg.com/[email protected]/dist/d3.min.js"></script>

CodePudding user response:

You should use Number.isNaN() instead. The global version of isNaN() has some confusing behavior.

When the argument to the isNaN function is not of type Number, the value is first coerced to a Number. The resulting value is then tested to determine whether it is NaN.

        var t = d3.timeParse("%Y-%m-%d %H:%M:%S.%L")("2022-06-19 12:20:30.200");
        v = d3.timeFormat("%m-%d %H:%M:%S.%L")(t)
        var isNan = Number.isNaN(v)
        console.log(isNan,v)
<script src="https://unpkg.com/[email protected]/dist/d3.min.js"></script>

  • Related