5 ways to convert string to number in JavaScript

featured Image

In this article, we will see how to convert a string to a number in JavaScript.

In Javascript, we can represent a number in the form of an actual number or as a string.

Example

let num1 = 20;
let num2 = '20'
console.log(typeof num1) // Number
console.log(typeof num2) // String

And if we compare them using === (equality comparison operator) , it will return as false since both are of different data types.

console.log(num1 === num2) //false

So, in this article, we will see 5 different ways on how to convert a given string to a number using vanilla Javascript.

Method 1 : Using parseInt()

The parseInt() method parses a given string argument and return us an integer i.e a whole number.

syntax:

parseInt(string, radix)

It takes two arguments:

string: The value to parse.

radix: This is the base number used in mathematical numeral systems, you can read about it (Radix – Wikipedia). A radix of 10 converts from a decimal number, 8 converts from octal, 16 from hexadecimal, and so on.

Example:

let str = '20'
let num = parseInt(str)
console.log(num) // 20

The limitation of the parseInt() method is that it cannot convert a decimal number. It will round off to the nearest integer value and return us the number.

So to convert a decimal number string to a number we have to use the next method.

Method 2: Using parseFloat()

The parseFloat() method, parse a string argument and return us the floating-point number (decimal number).

Syntax:

parseFloat(string)

Example:

let str1 = '3.14'
let str2 = '5.44abcd'
console.log(parseFloat(str1)) // 3.14
console.log(parseFloat(str2)) // 5.44

Method 3 : Using Number()

The Number() function converts a string to a number in Javascript.

Unlike parseInt() and parseFloat() which can convert only to integer or a floating-point value, the Number() method can convert a string and return both integer and a decimal number.

And if the value cannot be converted it returns NaN (Not a Number).

Syntax:

Number(string)

Example:

let str1 = '3.14'
let str2 = '2.1345'
let str3 = '5.44abcd'
console.log(Number(str1)) //3.14
console.log(Number(str2)) // 2.1345
console.log(Number(str3)) //NaN

Method 4: Using Math.floor()

The Math.floor() function returns a value which is the largest integer less than or equal to the given number. It returns 0 if the given value is null.

Syntax:

Math.floor(x)

Example:

console.log(Math.floor(4.95)); //4
console.log(Math.floor(4.05)); //4
console.log(Math.floor(4)); //4
console.log(Math.floor(-4.05)); //-5
console.log(Math.floor(null)); //0

Method 5: Using Unary Operator (+)

The unary plus operator + converts the operand into a number.

If we place the unary operator before a numerical value, it does nothing.

However, if is placed the operator before the string (non-numeric) value, it does the number conversion using the Number() function.

Example:

let str = '20';
console.log(+str); // 10

So, these are five different ways to transform a string value into a number in JavaScript. And all these methods work on any modern browser and IE6.


Related Articles:

Generate random string/characters in JavaScript

How can I do String interpolation in JavaScript?

Compare Two Strings in JavaScript | String Comparison

Javascript – Convert array to String (with and without commas)

Convert array to string with brackets and quotes in JavaScript

Replace all occurrences of a string in JavaScript

Related Posts

featured Image

Get the 10 characters from a string using JavaScript.

Here, in this article we will learn how to get the first 10 character from any string using JavaScript. Here we will be using JavaScript’s String method substring. What is…

Read more
featured Image

Convert date to long date format using JavaScript

In this article we will look into how to convert a date to a long date format using JavaScript. To convert it to a long format we will be using…

Read more
featured Image

Prevent body from scrolling when a modal is opened

Here, in this article, we will learn how to prevent the body from scrolling when a modal or a pop-up is opened using JavaScript. The scroll event is not cancelable….

Read more