The ultimate guide of converting strings to numbers in javascript
In every programming language there are some tips which doesn’t seem important. But being expert in that language undoubtedly related to knowing these small gotchas. I am going to learn and document them in javascript language and thereupon share here.
The first time that converting strings to numbers in javascript catched my eye was when I noticed using a + before a variable. This is the definition of unary plus operator from MDN:
The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn’t already. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. It can convert string representations of integers and floats, as well as the non-string values true, false, and null. Integers in both decimal and hexadecimal (“0x”-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate to NaN.
Here are some examples tested in chrome:
1 | +"1" // 1 |
There are other ways of converting strings to number and also there are other functions which automatically convert strings to numbers:
1 | parseInt("1000", 10) // 1000 |
Note 1: Avoiding radix (2nd parameter) in parseInt
could result different in different implementations [*].
Note 2: Double bitwise not (~~) can also convert string to its representing number, but it could result strange for large numbers. Actually, it will convert the string to 32bit integers and if the integer can not be represented in 32bits, it will wrap [*].