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

前端 未结 2 1700
暖寄归人
暖寄归人 2021-02-11 12:30

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

2条回答
  •  温柔的废话
    2021-02-11 13:16

    + 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).

提交回复
热议问题