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):
var a = "123"
var b = "123b"
if ( +a === +a ) // true
if ( +b === +b ) // false
How does this logic work internally in JavaScript to make this possible?
My question is not how to check if a string is a valid number – this is already answered here: Validate decimal numbers in JavaScript - IsNumeric(). I want to understand how the statement +a === +a
works.
NaN
. But I would useisNaN(+a)
for this – Alexey Ten May 12 '15 at 13:54NaN !== NaN
– devnull69 May 12 '15 at 13:55