Joined September 2012
·

Heitor Salazar

Tecnhology Maiar at Indefinido
·
Belo Horizonte, Minas Gerais
·
·
·

Posted to parseInt() can be dangerous over 1 year ago

Also if want 0 instead of NaN use ~~:

~~"42"        //=> 42
~~"042"       //=> 42
~~"the 42"     //=> 0    <- Advantage of using ~~
~~"42px"      //=> 0

+"42"         //=> 42
+"042"        //=> 42
+"the 42"      //=> NaN
+"42px"       //=> NaN

parseInt("42")         //=> 42
parseInt("042")        //=> 34 (Firefox) || 42 (Chrome)
parseInt("09")         //=> NaN (Firefox) || 9 (Chrome)
parseInt("the 42")      //=> NaN
parseInt("42px")       //=> 42  <- Advantage of using parseInt
Achievements
70 Karma
499 Total ProTip Views