I.Convert String to number 

1. Convert String to Integer : parseInt(string,radix) 

parseInt(string,radix)
  • string Required. The string to be parsed 
  • radix Optional. A number (from 2 to 36) that represents the numeral system to be used 
  • Return: An integer. If the first character cannot be converted to a number, NaN is returned
Some examples:
var num= parseInt('10')  //return 10 
var num=parseInt('10 today '); //also return 10
var num=parseInt('10.5') //return 10 
var num=parseInt('today is 10'); // return NaN
var num=parseInt(010);   //return 8 
var num=parseInt('010'); //return 10
var num=parseInt('10',2); //return 2
var num=parseInt("34 45 66"); //return 34
var num=parseInt("0x10"); //return 16

2.Convert String to Float: parseFloat(string) 

parseFloat(string)
  • string Required. The string to be parsed 
  • Return value: Number A floating point number. If the first character cannot be converted to a number, NaN is returned
Some examples:
var num= parseFloat('10')  //return 10
var num=parseFloat('10 today '); //also return 10
var num=parseFloat('10.5') //return 10.5
var num=parseFloat('today is 10'); // return NaN
var num=parseFloat(010);   //return 8 
var num=parseFloat("34 45 66"); //return 34
var num=parseFloat("0x10"); //return 0

II.Convert a number to a string

1.number.toString(radix)

number.toString(radix)
  • radix Optional. Which base to use for representing a numeric value. Must be an integer between 2 and 36. 2 - The number will show as a binary value 8 - The number will show as an octal value 16 - The number will show as an hexadecimal value