# How to convert a string to a number In javascript

Brijpal Sharma
javascript   Publish on:-September 29th, 2018 , Category:- Web Development

In this post, we learn how to convert a string to a number In javascript.

Today topic is  Convert A String To A Number In Javascript. We can use the Number object to convert the strings as the number. and we have another methods like parseInt() and parseFloat().

## How To Convert A String To A Number In Javascript

To start first create a new javascript file named test.js and add some code as flowing.

``````const data = Number('48155');

console.log(data);``````

Now, we need to start the node.js server using this flowing command.

``node server``

Node server count decimal as well.

An integer is a wrapper object and it can perform many operations. If we use the constructor (`new Number("14785")`) so it will return us a Number object instead of a number value.

## #parseInt()

The parseInt() function parse a string and return an integer (a whole number). but it will Only the first number in the string is returned!

``````let text = '21px';
let integer = parseInt(text, 10);

console.log(integer);``````

it will return log the 21 value.

## #parseFloat()

The parseFloat() function is quite different it converts a string into a point number (a number with decimal points). You can even pass in strings with random text in them.

``````// server.js

let text = '3.14pie';
let integer = parseFloat(text, 10);

console.log(integer);``````

## + Unary Operator

To use Unary Operator + before the string. unary plus (+) is used when the string is already in the form of an integer.

``````// server.js

let x = 6+"1000";

console.log(x);``````

It's return output is  61000.

## #Math.floor()

#math.floor is Similar to the + unary operator, but the difference is it returns the integer part.

``````// server.js

let floor = Math.floor("1000.51");
console.log(floor);``````

Mostly #math.floor() used when you want to change the float value to an integer then it might help you.

The answer of above will be 1000.

## #Browser Compatibility

This all three methods work fine in all modern browsers, and IE6 and up.

Finally see, How to convert a string to a number In javascript example tutorial is over.

Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...