Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  87] [ 6]  / answers: 1 / hits: 151182  / 12 Years ago, fri, october 12, 2012, 12:00:00

Any number, it's number. String looks like a number, it's number. Everything else, it goes NaN.



'a' => NaN
'1' => 1
1 => 1

More From » javascript

 Answers
14

There are 4 ways to do it as far as I know.


Number(x);
parseInt(x, 10);
parseFloat(x);
+x;

By this quick test I made, it actually depends on browsers.


https://jsben.ch/NnBKM


Implicit marked the fastest on 3 browsers, but it makes the code hard to read… So choose whatever you feel like it!


[#82590] Wednesday, October 10, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierceabnerc

Total Points: 430
Total Questions: 92
Total Answers: 102

Location: Faroe Islands
Member since Thu, Apr 8, 2021
3 Years ago
;