Convert a numeric string to a number in JavaScript
This post will discuss how to convert a numeric string to a number in JavaScript.
1. Using Number() function
A simple solution is to use the Number() function, that takes a string as the argument and returns a number converted from the string. If the string is not a valid number, it returns NaN. For example:
|
1 2 3 4 5 6 7 8 |
const str = '100'; // convert the string to number using Number() function const i = Number(str); console.log(i); // 100 console.log(typeof i); // number console.log(i instanceof Number); // false |
This function handles the decimal strings and the null value. If we add the new keyword, Number() returns a Number object instead of the primitive number.
|
1 2 3 4 5 6 7 8 |
const str = '100'; // convert the string to number using Number() constructor const i = new Number(str); console.log(i); // [Number: 100] console.log(typeof i); // object console.log(i instanceof Number); // true |
2. Using parseInt() function
The parseInt() function takes a string as the first argument and an optional radix (base) as the second argument, and returns an integer parsed from the string. If the string is not a valid number, it returns NaN (not a number). For example:
|
1 2 3 4 5 6 7 |
const str = '100'; // convert the string to number using parseInt() function with radix 10 const i = parseInt(str, 10); console.log(i); // 100 console.log(typeof i); // number |
This function is same as the Number.parseInt() function. This function it will ignore any leading or trailing whitespace in the string, and will stop parsing only when it encounters an invalid character.
|
1 2 3 4 5 |
const str = '10s'; const i = parseInt(str, 10); console.log(i); // 10 console.log(typeof i); // number |
3. Unary plus operator
We can use unary plus (+) operator to convert a string to a number by placing it before the string. It works similarly to the Number() function, but with a shorter syntax. It also handles the decimal strings, and returns NaN for non-numeric strings. For example:
|
1 2 3 4 5 6 7 |
const str = '100'; // convert to number using unary plus operator const i = +str; console.log(i); // 100 console.log(typeof i); // number |
This is the fastest and preferred way of converting a string into a number. The unary negation (-) can also be used in the following manner.
|
1 2 3 4 5 |
const str = '100'; const i = -(-str); console.log(i); // 100 console.log(typeof i); // number |
4. Using Math.floor() function
If the string is guaranteed to be a valid integer, we can use the Math.floor() function to convert a string to a number. This function will remove the fractional part from the string, if any. For example:
|
1 2 3 4 5 |
const str = '100'; const i = Math.floor(str); console.log(i); // 100 console.log(typeof i); // number |
That’s all about converting a numeric string to a number in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)