How does “+var === +var” work internally to verify if var is numeric?

前端 未结 1 1647
南笙
南笙 2021-02-11 12:14

Seeing this question: Is there a (built-in) way in JavaScript to check if a string is a valid number? and this: jsperf, one of the presented approaches is this (mutatis mutandis

相关标签:
1条回答
  • 2021-02-11 13:12

    + converts the value to a number.

    a gets converted to 123 and 123 === 123.

    b gets converted to NaN but NaN !== NaN (because NaN is never equal to another NaN according step 4a of the equality rules).

    0 讨论(0)
提交回复
热议问题