Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction 
2.
How to Convert String to Number in JavaScript?
3.
Approaches for the String to Number Javascript
3.1.
1. Using parseInt() Function
3.1.1.
Example
3.1.2.
Output 
3.2.
2. Using Number() Function
3.2.1.
Example
3.2.2.
Output 
3.3.
3. Using Unary Operator (+) Function
3.3.1.
Example
3.3.2.
Output 
3.4.
4. Using parseFloat() Function
3.4.1.
Example
3.4.2.
Output 
3.5.
5. Using Math.floor() Function
3.5.1.
Example
3.5.2.
Output 
3.6.
6. Multiply the String With Number 1
3.6.1.
Example
3.6.2.
Output 
3.7.
7. Dividing the String With Number 1
3.7.1.
Example
3.8.
Javascript
3.8.1.
Output
3.9.
8. Subtracting the Number 0 from The String
3.9.1.
Example
3.10.
Javascript
3.10.1.
Output
3.11.
9. Using the Bitwise NOT Operator (~)
3.11.1.
Example
3.12.
Javascript
3.12.1.
Output
3.13.
10. Using Double tilde (~~) Operator
3.13.1.
Example
3.13.2.
Output 
3.14.
11. Using the Math.ceil() Function
3.14.1.
Example
3.15.
Javascript
3.15.1.
Output
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: Mar 27, 2024
Easy

How to Convert String to Number in Javascript?

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction 

In programming, the data type of a variable frequently needs to be converted to another data type. This might be when a floating-point variable needs to be converted to an integer type and vice versa. There are multiple reasons why type conversions are implemented.

In this article, you will understand how to convert a String to 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!!

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Approaches for the String to Number 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.

Syntax parseInt( 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"))


Output 

output

2. Using Number() Function

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

Syntax Number(value)
Parameters value: 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"))


Output 

output

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)


Output 

output

4. Using parseFloat() Function

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

Syntax parseFloat(value)
Parameters value: 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"))


Output 

output

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.

Syntax Math.floor(value)
Parameters value: 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))


Output 

output

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)


Output 

output

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

  • Javascript

Javascript

let strNumber = "42";
let convertedNumber = strNumber / 1;
console.log(convertedNumber);

Output

output

8. Subtracting the Number 0 from The String

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

Example

  • Javascript

Javascript

let strNumber = "20";
let convertedNumber = strNumber - 0;
console.log(convertedNumber);

Output

output

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

  • Javascript

Javascript

let strNumber = "16";
let convertedNumber = ~strNumber;
console.log(convertedNumber);

Output

output

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)

Output 

output

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

  • Javascript

Javascript

let strNumber = "7";
let convertedNumber = Math.ceil(strNumber);
console.log(convertedNumber);

Output

output

Comparison 

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

Test Ops/sec
A var x = +"1000';

682, 485, 087

±0.87%

fastest

B var x = Number("1000");

681,561,350

±0.85%

fastest

C var x = Math.floor("1000");

94,940,907

±0.79%

86% slower

D var x = parseInt("1000");

213,309,107

±1.50%

69% slower

E var x = 1*"1000";

686,935,569

±1.06%

fastest

F var 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.

Refer to our Guided Path to upskill yourself in DSACompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, check out the mock test series and participate in the contests hosted on Coding Ninjas Studio!

Previous article
JavaScript String charCodeAt()
Next article
JavaScript String indexOf() method
Live masterclass