Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (7k points)

How do I convert a string to an integer in JavaScript?

1 Answer

0 votes
by (13.1k points)

You can do it in several ways:

By using the Number function

var i=Number(“100”);

parseInt:

var i=parseInt(“100”,10)->10 means decimal

Unary plus:
var i=+”100”;

If your string might be a float and you want a integer:

var i=Math.floor(“100.01”);

Or

var floor=Math.floor;

var i=floor(“100.01”);

You can use parseFloat:

var floor=Math.floor;

var i=floor(parseFloat(“100.23”));

Math.out like this:

var round=Math.round;

var i=round(“100”);

Want to be a full stack developer? Check out the Full stack developer course from Intellipaat.

Related questions

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.5k answers

500 comments

108k users

Browse Categories

...