What's the fastest way to convert String to Number in JavaScript?

Asked
Active3 hr before
Viewed126 times

9 Answers

stringconvert
90%

You can also use the - (minus) operator which type-converts the value into number but also negates it.,Converting strings to numbers is extremely common. The easiest and fastest (jsPerf) way to achieve that would be using the + (plus) operator.,The void operator returns an undefined value from an evaluated expression,The ternary operator is a shortcut for the if statement.

var one = '1';

var numberOne = +one; // Number 1
load more v
88%

There are 4 ways to do it as far as I know.,By this quick test I made, it actually depends on browsers.,This all depends on how the perf tests are configured as some show parseInt/parseFloat to be much slower. ,The fastest way is using -0:

There are 4 ways to do it as far as I know.

Number(x);
parseInt(x, 10);
parseFloat(x); +
x;
load more v
72%

Here is simple way to do it: var num = Number(str); in this example str is the variable that contains the string. You can tested and see how it works open: Google chrome developer tools, then go to the console and paste the following code. read the comments to understand better how the conversion is done. ,This is probably not that fast, but has the added benefit of making sure your number is at least a certain value (e.g. 0), or at most a certain value:,A fast way to convert strings to an integer is to use a bitwise or, like so: ,Please note: Integer overflow and/or bit truncation can occur with ~~, but not the other conversions. While it is unusual to be entering such large values, you need to be aware of this. Example updated to include much larger values.

Any number, it's number. String looks like a number, it's number. Everything else, it goes NaN.

'a' => NaN
   '1' => 1
1 => 1
load more v
65%

We begin with parseInt() which takes two arguments, a string and a radix. The function returns an integer parsed from the given string.,Only the first number in the string is returned.,parseFloat() is quite similar to parseInt(), with two main differences.,MDN suggests that when using parseInt(), you should “Always specify a radix to avoid… unreliable behavior.”

parseInt( )

parseInt(string, radix)

The parseInt() method starts at position 0 and determines if the character found there can be converted into a valid number. If not, the method returns NaN and stops. Here is an example:

const str = “What’ s funnier than 24 ? ”,
   console.log(parseInt(str)) // NaN

If the character at position 0 is valid, the method moves on and carries out the same test. This goes on until parseInt() encounters an invalid number, at which point it will convert the current string into a number.

console.log(parseInt(“24 haha25”)) // 24
load more v
75%

var myInt = parseInt("10.256"); //10
var myFloat = parseFloat("10.256"); //10.256
40%

.parseInt() takes a String as a first argument, and a base to which that String will be converted to. This method always returns an integer.,.parseFloat() takes a String as an argument, and returns the Float point number equivalent.,Unary Operator By adding a + sign before a String, it will be converted into a number if it follows the right format.,The method can accept Strings, also making it a way to convert a String to a number:

let str = '353';
let fltStr = '353.56';
let binStr = '7';
let nanStr = 'hello';

parseInt(str); // 353
parseInt(fltStr); // 353
parseInt(binStr, 2); // 111 (Binary)
parseInt(nanStr); // NaN (Not a Number)
load more v
22%

JavaScript provides various ways to convert a string value into a number.,The best one in my opinion is to use the Number object, in a non-constructor context (without the new keyword):,One “trick” is to use the unary operator + before the string:,parseInt() tries to get a number from a string that does not only contain a number:

const count = Number('1234') //1234
load more v
60%

We can use the double tilde operator to convert the string to number. ,Number() can be used to convert JavaScript variables to numbers. We can use it to convert the string too number. If the value cannot be converted to a number, NaN is returned. ,Multiplying the string value with the 1 which won’t change the value and also it will be converted to number by default. ,DEV Community – A constructive and inclusive social network for software developers. With you every step of your journey.

myString = '129'
console.log(parseInt(myString)) // expected result: 129

a = 12.22
console.log(parseInt(a)) // expected result: 12
load more v
48%

The parseInt() method converts a string into an integer (a whole number).,The Number() method converts a string to a number.,The parseFloat() method converts a string into a point number (a number with decimal points). You can even pass in strings with random text in them.,Today, let’s look at three different ways to convert a string into a number.

var num1 = 42;
var num2 = '42';
if (num1 === num2) {
   console.log(true);
} else {
   console.log(false);
}
// Will log `false`
load more v

Other "string-convert" queries related to "What's the fastest way to convert String to Number in JavaScript?"