How to convert string to int or number in JavaScript

Key takeaways:

  • JavaScript provides several methods for converting strings to numbers, including parseInt(), parseFloat(), Number(), the unary + operator, multiplication by 1, and the double tilde (~~) operator.

  • Each method has specific use cases, so understanding their differences and nuances is important for accurate and efficient coding.

  • Converting between data types is essential in programming, allowing us to manipulate and validate data.

In programming languages, data type conversion is an important aspect since it enables the manipulation of data in a variety of ways. Several programming operations (e.g., input validation and mathematical operations) require this conversion. In this Answer, we’ll understand some of the most popular methods for converting string to int or number in JavaScript.

Converting a string into a number in JavaScript

JavaScript provides several ways to handle this conversion, each with unique benefits. Let’s learn various methods to convert a string to an integer or number in JavaScript, and discuss when to use each method effectively.

Method 1: Using the parseInt() method

The parseInt() function of JavaScript turns a string input into an integer. The parseInt() function will truncate any decimal portions and round the result to the nearest whole number if the input string contains a decimal value. If the string contains non-numeric characters, parseInt() will stop parsing and return the numeric value parsed up to that point. If the first character of the string is not a valid number, parseInt() will return NaN.

Syntax

The parseInt() method has the following syntax:

parseInt(string, radix);

Here:

  • string: The string to be converted.

  • radix: The base of the numeral system to be used. This is optional but is highly recommended, as omitting it can lead to unexpected results.

Code example

Let’s look at the code below:

let my_string ='123';
console.log(parseInt(my_string, 10));
my_string='12.3';
console.log(parseInt(my_string, 10));
my_string="34skaj7akj";
console.log(parseInt(my_string, 10));
my_string="ska23j7akj";
console.log(parseInt(my_string, 10));

In the above code:

  • Line 2: We pass a string containing only integer values and 10 as the base to the parseInt() method. This method then converts the string to the corresponding integer value in base 10, in this case, 123.

  • Line 5: Here, we pass a string containing a decimal value to the parseInt() method. Since parseInt() only works with integers, it will round the decimal value to the nearest whole number.

  • Line 8: In this case, we pass a string containing non-numeric characters, parseInt() will stop parsing as soon as it reaches the first non-numeric character, and return the numeric value parsed up to that point.

  • Line 11: Finally, we pass a string whose first character is not a valid number to the parseInt() method, it will return NaN.

The parseFloat() function is similar to parseInt(), but it parses the entire number, including any decimal portion. It’s ideal when we need to convert a string to a floating-point number.

Method 2: Using the Number() method

The Number() function converts the string into a number based on the contents of the string. This method will return NaN if the string cannot be converted to a number.

Code example

Let’s look at the code below:

let my_string = '123';
console.log(Number(my_string));
my_string=' 123 ';
console.log(Number(my_string));
my_string='12.3';
console.log(Number(my_string));
my_string='12.3kljhg';
console.log(Number(my_string));

In the above code:

  • Line 2: We pass a string containing only integer values to the Number() method. The method then converts the string to the corresponding integer value, in this case, 123.

  • Line 5: We pass a string with extra spaces around the number to the Number method. This method then converts the string to the corresponding integer value and also removes extra spaces from the string.

  • Line 8: Here, we pass a string containing a decimal value to the Number() method. So, it converts this string to a numeric value.

  • Line 11: In this case, we pass a string containing non-numeric characters. The Number() method will return NaN as the input string is not a valid numeric value.

Method 3: Using the unary operator method

This method uses the unary plus (+) operator to convert its operand to a number. It works similarly to the Number() function and can handle both integers and floating-point numbers.

Code example

Let’s look at the code below:

let my_string = '123';
console.log(+my_string);
my_string=' 123 ';
console.log(+my_string);
my_string='12.3';
console.log(+my_string);
my_string='12.3jsh';
console.log(+my_string);

In the above code:

  • Line 2: We pass a string containing only integer values to the unary operator. This method then converts the string to the corresponding integer value, in this case, 123.

  • Line 5: We pass a string with extra spaces around the number, and the unary operator method then converts the string to the corresponding integer value and also removes extra spaces from the string.

  • Line 8: Here, we pass a string containing a decimal value to the unary operator method, and it converts this string to a numeric value.

  • Line 11: In this case, we pass a string containing non-numeric characters, and this method returns NaN as the input string is not a valid numeric value.

Method 4: Multiplication with number

This method multiplies the string with the number 1 and converts it into a number.

Code example

Let’s look at the code below:

let my_string = '123';
console.log(my_string*1);
my_string=' 123 ';
console.log(my_string*1);
my_string='12.3';
console.log(my_string*1);
my_string='12.3kj';
console.log(my_string*1);

In the above code:

  • Line 2: We multiply a string containing only integer values with 1, and it converts the string to the corresponding integer value, in this case, 123.

  • Line 5: We multiply a string containing extra spaces around the number with 1, and it then converts the string to the corresponding integer value and also removes extra spaces from the string.

  • Line 8: Here, we multiply a string containing a decimal value with 1, and it converts this string to a numeric value.

  • Line 11: In this case, we multiply a string containing non-numeric characters with 1, and this returns NaN as the input string is not a valid numeric value.

Method 5: The double tilde (~~) operator method

In JavaScript, the double tilde (~~) operator can be used to convert a string to a number, but this method will truncate any decimal portions if the input string contains a decimal value.

Code example

Let’s look at the code below:

let my_string = '123';
console.log(~~my_string);
my_string=' 123 ';
console.log(~~my_string);
my_string='12.7';
console.log(~~my_string);
my_string='12lkj';
console.log(~~my_string);

In the above code:

  • Line 2: We pass a string containing only integer values to the ~~ operator and it converts the string to the corresponding integer value, in this case, 123.

  • Line 5: We pass a string with extra spaces around the number to the ~~ operator, which converts the string to the corresponding integer value and removes extra spaces from the string.

  • Line 8: Here, we pass a string containing a decimal value to the ~~ operator, and it removes the decimal part of the number and returns the integer part.

  • Line 11: In this case, we pass a string containing non-numeric characters to the ~~ operator, and this returns 0 as the input string is not a valid numeric value.

Which method should you choose?

Each of the above methods for converting a string to an integer or number in JavaScript is suitable for different cases. Here’s a quick guide to help you decide which method to use based on your needs:

  • Use parseInt() if you need an integer and want to ignore any decimal part. This method is also useful if you want to specify a radix (base) for conversions (e.g., base 10 for decimal).

  • Use parseFloat() if you need a floating-point number and want to preserve any decimal part.

  • Use Number() or the unary + operator if you want to handle both integers and floats automatically. These methods are useful when you need to convert directly to a number regardless of the format of the input string.

  • Use multiplication by 1 or the ~~ (double tilde) operator if you’re looking for a quick and concise conversion, though ~~ truncates decimals and should be avoided if you need precision.

Choosing the appropriate method for string-to-number conversion can ensure that your JavaScript code works smoothly without unnecessary errors from unexpected data types.

Knowledge test

Let’s attempt a short quiz to assess your understanding.

Q

What will be the output of the following code?

let num = parseInt("123.456");
console.log(num);
A)

123

B)

123.456

C)

456

D)

NaN


Conclusion

JavaScript offers multiple methods to convert a string into an integer or number, each with unique capabilities. Choosing the right method can make the code more efficient and accurate, especially when handling data input, validation, or calculations. By learning these conversion methods, one can improve the ability to handle various data types effectively, creating smoother, more reliable JavaScript applications.


Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to convert a string to a number in JavaScript prompt?

If you’re capturing user input with a prompt and want to convert it to a number, you can apply one of the conversion methods directly to the result. For example:

let userInput = prompt("Enter a number:");
let number = Number(userInput); // or use parseInt(userInput) for integers

Can we convert a string value into a number?

Yes, JavaScript provides multiple methods to convert a string to a number. Common options are parseInt() for integers, parseFloat() for floating-point numbers, and Number() or the unary + for handling both integers and floats. Each method has specific use cases, so it’s important to understand their differences.


How to convert a string to a number without rounding in JavaScript?

To avoid rounding and preserve any decimal part of the number, you should use parseFloat() or Number(). Both will keep the decimal values intact. For example:

let number = parseFloat("123.456"); // returns 123.456 without rounding

Avoid parseInt() or ~~ (double tilde) if you need to keep the decimal part, as these will truncate it to an integer.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved