Basic JavaScript Question:
Download Questions PDF

How to convert a string to a number using JavaScript?

Answer:

We can use the parseInt() and parseFloat() methods in JavaScript to convert a string to a number or numeric value. Notice that extra letters following a valid number are ignored, which is kinda wierd but convenient at times.

parseInt("100") ==> 100
parseFloat("98.6") ==> 98.6
parseFloat("98.6 is a common temperature.") ==> 98.6
parseInt("aa") ==> Nan //Not a Number
parseInt("aa",16) ==> 170 //We can supply a radix or base

Download JavaScript Interview Questions And Answers PDF

Previous QuestionNext Question
How to force a page to go to another page using JavaScript?How to convert numbers to strings using JavaScript?