There are several ways you can convert JavaScript String to Number. The best way is to make use of the unary plus +
operator or the Number global function. You can also make use of the parseint
or parsefloat
functions. The following examples show how to make use of these functions.
Unary plus (+)
The Unary plus (+) operator converts all string representations of numbers, Boolean values(true and false), and null to numbers. If the operand cannot be converted into a number, the unary plus operator will return NaN
The following are some of the examples
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | console.log(+"100") //100 console.log(+"100.5175") //100.5175 console.log(+"10AA0.5175") //NaN console.log(+"") //0 console.log(+" ") //0 console.log(+null) //0 console.log(+undefined) //Nan console.log(+Infinity) //Infinity console.log(+true) //1 console.log(+false) //0 console.log(+"0x22") //34 console.log(+"0022") //22 console.log(+"0o51") //41 console.log(+"3.125e7") //31250000 console.log(+"35 35") //NaN console.log(+"AB 35") //NaN |
- Tries to convert any string to a number
- It converts empty string /
Null
to 0 True
is 1 andFalse
is 0- Converts Octal/Hex numbers to decimal.
- Works correctly on scientific notation numbers.
- In case it fails to convert, then returns
NaN
Parseint
The parseint
function parses an string and returns an integer. The syntax of the parseInt
is as follows. If you do not provide the radix
then it assumes the Hexadecimal number.
parseInt(string, radix)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | console.log(parseInt("100")) //100 console.log(parseInt("100.5175")) //100 console.log(parseInt("10AA0.5175")) //10 console.log(parseInt("")) //NaN console.log(parseInt(null)) //NaN console.log(parseInt(Infinity)) //NaN console.log(parseInt(true)) //NaN console.log(parseInt(false)) //NaN console.log(parseInt("0x22")) //34 console.log(parseInt("0022")) //22 console.log(parseInt("0o51")) //0 console.log(parseInt("3.125e7")) //3 console.log(parseInt("35 35")) //35 console.log(parseInt("AB 35")) //NaN |
- Tries to convert any string to a integer and not to floating number
- Converts empty strings, null, infinity, True & false to a
NaN
- If the string begins with
0x
then it treats it a Hexadecimal number. - It does not recognize the octal number that begins with
0o
- The older browsers will treat the number starting with
0
as Octal. - Ignores the Leading & trailing spaces
- If only the initial part of the string is a number, then it converts into number and ignores the rest.
You can make use of the radix
. The radix
can be from 2 to 36. Use 2
for binary, 8
for octal, 10
for decimal & 16
for HexaDecimal number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | console.log(parseInt("51")); //51 console.log(parseInt("51",8)); //41 console.log(parseInt("51",16)); //81 console.log(parseInt("051")); //51 console.log(parseInt("051",8)); //41 console.log(parseInt("051",16)); //81 console.log(parseInt("0x51")); //81 console.log(parseInt("0x51",8)); //0 console.log(parseInt("0x51",16)); //81 console.log(parseInt("0o51")); //0 console.log(parseInt("0o51",8)); //0 console.log(parseInt("0o51",16)); //0 |
Parsefloat
ParseFloat
is another way to convert string to a number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | console.log(parseFloat("100")) //100 console.log(parseFloat("100.5175")) //100.5175 console.log(parseFloat("10AA0.5175")) //10 console.log(parseFloat("")) //NaN console.log(parseFloat(null)) //NaN console.log(parseFloat(Infinity)) //Infinity console.log(parseFloat(true)) //NaN console.log(parseFloat(false)) //NaN console.log(parseFloat("0x22")) //0 console.log(parseFloat("0022")) //22 console.log(parseFloat("0o51")) //0 console.log(parseFloat("3.125e7")) //31250000 console.log(parseFloat("35 35")) //35 console.log(parseFloat("AB 35")) //NaN |
- Tries to convert any string to a decimal or an integer number
- Converts empty strings, null, True & false to a
NaN
- Returns infinity as it is.
- It the string begins with 0x or 0o it returns zero. Does not recognize Hexa decimal or octal numbers
- Ignores the Leading & trailing spaces
- If only the initial part of the string is a number, then it converts into number and ignores the rest.
Number global function
You can also use the Number
global function. It is very similar to unary plus (+
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | console.log(Number("100")) //100 console.log(Number("100.5175")) //100.5175 console.log(Number("10AA0.5175")) //NaN console.log(Number("")) //0 console.log(Number(" ")) //0 console.log(Number(null)) //0 console.log(Number(undefined)) //Nan console.log(Number(Infinity)) //Infinity console.log(Number(true)) //1 console.log(Number(false)) //0 console.log(Number("0x22")) //34 console.log(Number("0022")) //22 console.log(Number("0o51")) //41 console.log(Number("3.125e7")) //31250000 console.log(Number("35 35")) //NaN console.log(Number("AB 35")) //NaN |
- Tries to convert any string to a number
- It converts empty string /
Null
to 0 True
is 1 andFalse
is 0- Converts Octal/Hex numbers to decimal.
- Works correctly on scientific notation numbers.
- In case it fails to convert, then returns
NaN
Check for NaN
The string to number operation may result in a NaN
. The NaN
stands for not a number. It is the result of numerical operations, where the result is not a number. Hence always check if the value is NaN
using the isNaN
method after the conversion.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | convertToNumber("AB 35") convertToNumber("35") function convertToNumber(numVal) { if (isNaN(+numVal)) { console.log("Number is NaN") } else { console.log(+numVal) } } //Output Number is NaN 35 |
References
Read More