How to convert string to number JavaScript?


In this short tutorial we are going to find out how to convert string to number with JavaScript. We will look at the different methods and their syntax with examples.

Table of Contents - String to number JavaScript

  • Introduction
  • Using the parseInt method to convert a string to number
  • Using the parseFloat method to convert a string to number
  • Multiply by 1 method
  • Closing thoughts
  • Other Related Concepts

Introduction

Managing data is a core, fundamental concept of programming. And converting a string to number using JavaScript is a commonly used, simple operation.

In JavaScript, you can represent a number as an integer (example: 7) or even as a string (example: “7”). But if we use a strict comparison method to evaluate the two, it would fail because they are two totally different types of objects.

Example:

var number1 = 7;
var number2 = '7';
if (num1 === num2) {
    console.log(true);
} 
else {
    console.log(false);
}
// It will log as false


Using the parseInt method to convert a string to number

JavaScript provides us with several methods to easily transfer a string to a primitive number. Let’s look at the parseInt() method to convert a string to number in JavaScript.

The parseInt() method takes a string as the first argument. It also takes a base to which the string will be converted. The returned value will always be an integer.

Syntax:

parseInt(string, radix)

A radix parameter specifies the number system to use: 2 = binary, 8 = octal, 10 = decimal, 16 = hexadecimal.
If radix is omitted, JavaScript assumes radix 10. If the value begins with "0x", JavaScript assumes radix 16.

Input:

var numb = '7';
var integer = parseInt(numb, 10);
        
console.log(integer)

Output:

7

Using the parseFloat method to convert a string to number

In this method, we see that JavaScript converts a string to a point number. A point number is a number with decimal points. We can also pass strings with random text in it and JavaScript will return the value.

Input:

var ranNumber = '7.14HireFreelanceDevelopersHere';
var pointNumber = parseFloat(ranNumber);
        
console.log(pointNumber)

Output:

7.14

Multiplication by 1 method

This method is arguably the fastest method to arrive at our converted values.

Input:

let stri = '777';
let flotStri = '777.1457';
let nanStri = 'flexiple';
        
stri * 1;      
flotStri * 1;  
nanStri * 1;

console.log(stri)
console.log(flotStri)
console.log(nanStri)

console.log('7000' * 1); 
console.log('93.5' * 1);

Output:

777
777.1457
flexiple
7000
93.5

In this method, we have multiplied our strings with 1 to have it return to us in integer or point number format. Alternatively we can also use the addition ‘+’ operation to arrive at the same result.

Closing thoughts

In this tutorial, we have seen a few different methods of converting strings to numbers in Javascript. Though there are many valid ways of manipulating data. It is up to the programmer to decide which one they prefer; choosing performance over readability or a balance between the available options.