Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to convert a string to a number in JavaScript

Introduction

In this tutorial, we are going to explore how to convert a string to a number in JavaScript. We'll be discussing several methods and their use cases, with easy-to-understand examples.

JavaScript is a powerful language when it comes to handling data types, and it provides us with a variety of ways to perform operations with those data types. In particular, converting a string to a number is a common scenario that developers encounter.

Before we dive into the methods, let's quickly understand what a string is and what a number is in JavaScript.

Strings and Numbers

In JavaScript, a string is a sequence of characters enclosed within single or double quotes. For example:

let name = "John";
let age = "25";

A number is a numerical data type that can be an integer (whole number) or a floating-point number (decimal number). For example:

let age = 25; // integer
let height = 5.9; // floating-point number

Now that we have a basic understanding of strings and numbers in JavaScript, let's dive into the methods to convert a string to a number.

Method 1: The Number() Function

The Number() function is a built-in JavaScript function that can be used to convert a string to a number. It takes a single argument, which is the value you want to convert to a number.

Here's an example of how to use the Number() function:

let age = "25";
let ageAsNumber = Number(age);
console.log(ageAsNumber); // Output: 25

In this example, we have a string variable age with the value "25". We pass this variable to the Number() function, which returns the value as a number. We then store this number in a new variable called ageAsNumber.

The Number() function is quite versatile, as it can handle both integer and floating-point numbers:

let height = "5.9";
let heightAsNumber = Number(height);
console.log(heightAsNumber); // Output: 5.9

However, it's important to note that if the string contains any non-numeric characters, the Number() function will return NaN (Not a Number):

let invalidNumber = "25a";
let result = Number(invalidNumber);
console.log(result); // Output: NaN

Method 2: The parseInt() Function

The parseInt() function is another built-in JavaScript function that can be used to convert a string to an integer. It takes a string as its first argument and an optional radix (base) as its second argument.

The parseInt() function will attempt to parse the string from left to right and stop when it encounters a non-numeric character. It will then return the integer value that it has parsed.

Here's an example of how to use the parseInt() function:

let age = "25";
let ageAsNumber = parseInt(age);
console.log(ageAsNumber); // Output: 25

In this example, the parseInt() function successfully converts the string "25" to the integer 25.

However, if the string contains non-numeric characters, the parseInt() function will stop parsing at the first non-numeric character:

let stringWithText = "25 years";
let parsedNumber = parseInt(stringWithText);
console.log(parsedNumber); // Output: 25

In this case, the parseInt() function successfully extracts the integer 25 from the string "25 years".

It's important to note that the parseInt() function can only handle integer values. If you want to parse floating-point numbers, you should use the parseFloat() function, which we'll discuss next.

Method 3: The parseFloat() Function

The parseFloat() function is another built-in JavaScript function that can be used to convert a string to a floating-point number. It takes a single argument, which is the string you want to parse.

Like the parseInt() function, the parseFloat() function will attempt to parse the string from left to right and stop when it encounters a non-numeric character. It will then return the floating-point number that it has parsed.

Here's an example of how to use the parseFloat() function:

let height = "5.9";
let heightAsNumber = parseFloat(height);
console.log(heightAsNumber); // Output: 5.9

In this example, the parseFloat() function successfully converts the string "5.9" to the floating-point number 5.9.

However, if the string contains non-numeric characters, the parseFloat() function will stop parsing at the first non-numeric character:

let stringWithText = "5.9 meters";
let parsedNumber = parseFloat(stringWithText);
console.log(parsedNumber); // Output: 5.9

In this case, the parseFloat() function successfully extracts the floating-point number 5.9 from the string "5.9 meters".

Method 4: The Unary Plus Operator

The unary plus operator (+) is another way to convert a string to a number in JavaScript. It is a lesser-known method but can be very useful in certain situations.

To use the unary plus operator, simply place a + symbol before the string you want to convert. Here's an example:

let age = "25";
let ageAsNumber = +age;
console.log(ageAsNumber); // Output: 25

In this example, we use the unary plus operator to convert the string "25" to the number 25.

The unary plus operator can also handle floating-point numbers:

let height = "5.9";
let heightAsNumber = +height;
console.log(heightAsNumber); // Output: 5.9

However, if the string contains non-numeric characters, the unary plus operator will return NaN:

let invalidNumber = "25a";
let result = +invalidNumber;
console.log(result); // Output: NaN

Conclusion

In this tutorial, we have explored four different methods to convert a string to a number in JavaScript:

  1. The Number() function
  2. The parseInt() function
  3. The parseFloat() function
  4. The unary plus operator

Each method has its own use cases and limitations, but they can all be used to achieve the conversion of a string to a number. Depending on your specific requirements, you can choose the method that best fits your needs.

Remember to practice these methods and experiment with different scenarios to gain a better understanding of how they work. Happy coding!