Table of contents
1.
Introduction 
2.
How to Convert String to Number in JavaScript?
3.
Approaches to Convert String to Number in Javascript
3.1.
1. Using parseInt() Function
3.2.
2. Using Number() Function
3.3.
3. Using Unary Operator (+) Function
3.4.
4. Using parseFloat() Function
3.5.
5. Using Math.floor() Function
3.6.
6. Multiply the String With Number 1
3.7.
7. Dividing the String With Number 1
3.8.
8. Subtracting the Number 0 from The String
3.9.
9. Using the Bitwise NOT Operator (~)
3.10.
10. Using Double tilde (~~) Operator
3.11.
11. Using the Math.ceil() Function
4.
Comparison 
5.
Frequently Asked Questions 
5.1.
What is the difference between parseInt() and parseFloat() in JS?
5.2.
What is the fastest way to convert string to number in JavaScript?
5.3.
Does JavaScript automatically convert string to number?
5.4.
How to convert empty string to number in JavaScript?
6.
Conclusion 
Last Updated: Jan 7, 2025
Easy

Convert String to Number in Javascript

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

In programming, it’s common to convert a variable's data type, such as changing a floating-point number to an integer or vice versa. This type conversion serves various purposes, including ensuring compatibility and optimizing performance. In this article, we will explore methods for converting a string to a number in JavaScript.

string to number javascript

How to Convert String to Number in JavaScript?

In JavaScript, there are many built-in methods to convert string to number. It also has unary operator support to convert string to number. 

The types of conversion in JavaScript to convert the string to number are:-

  1. Implicit Conversion: In this method, the unary operator (+) is used before the string to convert it into number.
     
  2. Explicit Conversion: All the operations did not automatically convert string to number in JavaScript. In this method, there are many in-built methods namely parseInt(), Number(), and parseFloat().

 

Now let us see all the string to number conversions in deep detail!!

Approaches to Convert String to Number in Javascript

There are 11 ways to convert a string to number in Javascript. Let us discuss each one by one: 

1. Using parseInt() Function

The parseInt method parses a string value and returns the first integer. Leading and trailing spaces are not considered.

SyntaxparseInt( string, radix)
Parameters

 

String: Required. The value to be parsed

Radix: Optional. A radix parameter specifies the number system to be used:

2 = binary, 8 = octal, 10 = decimal, and 16 = hexadecimal.

Note: If radix is not specified, JavaScript assumes radix 10.

 

Return It returns a number. NaN is returned if the first character cannot be converted.

Example

var myString = "10783"
console.log(parseInt(myString, 10))
console.log(parseInt("10"))
console.log(parseInt("10.33"))
console.log(parseInt("34 45 66"))
console.log(parseInt("   90   "))
console.log(parseInt("I am 20"))
You can also try this code with Online Javascript Compiler
Run Code


Output 

10783
10
10
34
90
nan

2. Using Number() Function

The Number() method is used to convert a value to a number.

SyntaxNumber(value)
Parametersvalue: Optional. The Number() function takes an object argument and converts it to a number that represents the object's value.
Return It returns a number. NaN is returned if the value cannot be converted.

 

Example

console.log(Number("10.33"))
console.log(Number(true))
console.log(Number("Ninja"))
You can also try this code with Online Javascript Compiler
Run Code


Output 

10.33
1
nan

3. Using Unary Operator (+) Function

It can convert null, boolean values (true and false), and all string representations of numbers into numbers. Integers, floats, hexadecimal, scientific (exponent) notation, and Infinity are acceptable numbers.

Syntax+value
Return NaN will be returned if the operand cannot be converted into a number.

 

Example

console.log(+"10.33")
console.log(+"Infinity")
console.log(+true)
You can also try this code with Online Javascript Compiler
Run Code


Output

10.33
Infinity
1 

4. Using parseFloat() Function

The parseFloat() method parses the string and returns the first number. 

SyntaxparseFloat(value)
Parametersvalue: Required. The value to be parsed
Return It returns a number. NaN is returned if the value cannot be converted.

 

Example

console.log(parseFloat("00"))
console.log(parseFloat(" 40 "))
console.log(parseFloat("Ninja"))
You can also try this code with Online Javascript Compiler
Run Code


Output 

0
40
nan

5. Using Math.floor() Function

The function Math.floor() returns the largest integer that is less/equal to a given number. This can be tricky with decimal numbers because it will return the nearest integer value as Number.

SyntaxMath.floor(value)
Parametersvalue: Required. The value to be parsed. 
Return It returns a largest integer that is less than or equal to the parsed value. NaN if no number is found.

 

Example

str = '5555'
console.log(Math.floor(str))
a = '78.22'
console.log(Math.floor(a))
You can also try this code with Online Javascript Compiler
Run Code


Output 

5555
78

6. Multiply the String With Number 1

To convert a string to a number in JavaScript using the method "Multiply the String With Number 1," you essentially use the implicit type coercion feature. By multiplying the string with the number 1, JavaScript coerces the string into a numeric value. This technique takes advantage of the fact that arithmetic operations in JavaScript typically work with numbers, triggering automatic type conversion. 

Example

str = '5555'
console.log(str * 1)
a = '78.22'
console.log(a * 1)
You can also try this code with Online Javascript Compiler
Run Code


Output 

5555
78.22

7. Dividing the String With Number 1

This involves performing a division operation by dividing the string with the number 1. JavaScript automatically coerces the string to a numeric value during the operation.

Example

let strNumber = "42";
let convertedNumber = strNumber / 1;
console.log(convertedNumber);
You can also try this code with Online Javascript Compiler
Run Code


Output

42

8. Subtracting the Number 0 from The String

By subtracting 0 from the string, JavaScript implicitly converts the string to a number.

Example

let strNumber = "20";
let convertedNumber = strNumber - 0;
console.log(convertedNumber);
You can also try this code with Online Javascript Compiler
Run Code


Output

20

9. Using the Bitwise NOT Operator (~)

Applying the bitwise NOT operator (~) is an unconventional method. It converts the string to a 32-bit signed integer. Keep in mind that it might not be suitable for all scenarios, as it involves bitwise operations.

Example

let strNumber = "16";
let convertedNumber = ~strNumber;
console.log(convertedNumber); 
You can also try this code with Online Javascript Compiler
Run Code


Output

-17

10. Using Double tilde (~~) Operator

With the help of a tilde operator, we can convert a string to number in Javascript. 

Example

str = '5555'
console.log(~~str)
a = '78.22'
console.log(~~a)
You can also try this code with Online Javascript Compiler
Run Code


Output 

5555
78.22

11. Using the Math.ceil() Function

While Math.ceil() is typically used for rounding up numbers, applying it to a string can lead to implicit conversion. However, this method might not be as straightforward or commonly used as the others.

Example

let strNumber = "7";
let convertedNumber = Math.ceil(strNumber);
console.log(convertedNumber);
You can also try this code with Online Javascript Compiler
Run Code


Output

7

Comparison 

You can refer to the image below to measure the performance of each method in comparison with each other. 

TestOps/sec
Avar x = +"1000';

682, 485, 087

±0.87%

fastest

Bvar x = Number("1000");

681,561,350

±0.85%

fastest

Cvar x = Math.floor("1000");

94,940,907

±0.79%

86% slower

Dvar x = parseInt("1000");

213,309,107

±1.50%

69% slower

Evar x = 1*"1000";

686,935,569

±1.06%

fastest

Fvar x = ~~"1000";

686,712,736

±0.88%

fastest

Read More About, Basics of Javascript
Let us now discuss some faqs based on the discussion above. 

Frequently Asked Questions 

What is the difference between parseInt() and parseFloat() in JS?

The only difference between parseInt() and parseFloat() is their parsing behaviour, not their return values. For example: parseInt(22.5) will return 22, whereas when using parseFloat(22.5) will return 22.5. 

What is the fastest way to convert string to number in JavaScript?

The fastest way to convert a string to a number in JavaScript is often using the unary plus operator (+) or Number() function.

Does JavaScript automatically convert string to number?

Yes, JavaScript can convert string to number automatically using a process called as implicit type conversion. But some operations require explicit conversion using the Number(), parseInt(), parseFloat() functions.

How to convert empty string to number in JavaScript?

To convert an empty string to a number in JavaScript, use parseFloat("") or parseInt(""), both resulting in NaN (Not a Number).

Conclusion 

To conclude the article, we have extensively discussed How to convert string to number in Javascript. We discussed 7 ways to convert a string to number with live examples. 

This blog has helped you enhance your knowledge of conversion between a string to number in Javascript. If you want to learn more, then check out our articles.

Live masterclass