How to Convert a String to a Number in JavaScript

Estimated read time 3 min read

Introduction to String to Number Conversion

In JavaScript, you often encounter scenarios where you need to convert a string to a number. This conversion is essential for performing mathematical operations or comparing values numerically. JavaScript provides several methods to convert strings to numbers, each with its own advantages and use cases. In this article, we will explore different techniques to convert a string to a number in JavaScript.

Using the parseInt Function

One of the most commonly used methods to convert a string to an integer is the parseInt function. The parseInt function parses a string and returns an integer based on the provided radix. Here’s an example:

const str = "42";
const num = parseInt(str);

console.log(num);  // Output: 42

In this example, we use the parseInt function to convert the string "42" to an integer. The resulting value is assigned to the num variable.

Using the parseFloat Function

If you need to convert a string to a floating-point number, you can use the parseFloat function. The parseFloat function works similarly to parseInt but returns a floating-point number. Here’s an example:

const str = "3.14";
const num = parseFloat(str);

console.log(num);  // Output: 3.14

In this example, we use the parseFloat function to convert the string "3.14" to a floating-point number. The resulting value is assigned to the num variable.

Using the Number Constructor

JavaScript provides the Number constructor, which can be used to create a number object from a given value. When used as a function without the new keyword, the Number constructor converts the value to a primitive number. Here’s an example:

const str = "123";
const num = Number(str);

console.log(num);  // Output: 123

In this example, we use the Number constructor to convert the string "123" to a number. The resulting value is assigned to the num variable.

Using the Unary Plus Operator

An alternative approach to convert a string to a number is by using the unary plus operator (+). The unary plus operator converts its operand to a number. Here’s an example:

const str = "99";
const num = +str;

console.log(num);  // Output: 99

In this example, we use the unary plus operator to convert the string "99" to a number. The resulting value is assigned to the num variable.

Using the Global parseInt and parseFloat Functions

Apart from the parseInt and parseFloat functions, JavaScript also provides global functions with the same names. These global functions behave similarly to their respective methods, but they are not attached to the Number object. Here’s an example:

const str = "42";
const num = window.parseInt(str);

console.log(num);  // Output: 42

In this example, we use the global parseInt function to convert the string "42" to an integer.

Handling Invalid Strings

When converting a string to a number, it’s important to consider cases where the string cannot be parsed as a valid number. In such situations, the conversion will result in NaN (Not a Number). You can use the isNaN function to check if a value is NaN. Here’s an example:

const str = "hello";
const num = parseInt(str);

if (
Mark Stain

My name is Mark Stein and I am an author of technical articles at EasyTechh. I do the parsing, writing and publishing of articles on various IT topics.

You May Also Like

More From Author

+ There are no comments

Add yours