By design, strings deal with text-based things. Numbers deal with...um...numbers. For the most part, unless you are displaying some text that includes numerical data, you will never need to mix and match numbers and strings together. That would just be weird and possibly unnecessary:

Of course, this wouldn't be HTML if you didn't find yourself doing unnecessary or weird things all the time. There are many cases where something that should be a number is actually returned to you as a string. If you've ever worked with data returned from a web service, fiddled with setting or getting CSS property values, or fell into any other situation where the data you are working with isn't entirely under your control, you totally know what I am talking about.
In this short tutorial, you will learn some simple tricks on how to convert a string into a number so that you can do all sorts of number-ish things to it.
Onwards!
Meet parseInt
Let's start with a very contrived situation that drives the point across:
var numberOne = "45"; var numberTwo = 3; alert(numberOne + numberTwo);
What do you think is going to be displayed when you add numberOne and numberTwo? Well, here is what gets displayed:

This probably isn't what you wanted. Here, numberOne is a string and numberTwo is a number. With this string operand, + joins their text instead of adding two quantities. The scary part is how subtle this is: a variable's name and a digit-looking value do not establish a numeric input contract.
We need to validate the input and convert it before adding. Converting after joining the digits would not recover the original quantities. For this known integer text, parseInt reads the integer prefix. That makes it useful here, but accepting a prefix is not the same as validating an entire field:
var numberOne = parseInt("45");
var numberTwo = 3;
alert(numberOne + numberTwo);
This time around, because both numberOne (thanks to parseInt) and numberTwo are numbers, the value you will see is 48.
Don't Forget About parseFloat!
If the text contains a decimal fraction, parseInt stops at the decimal point. For "2.99", it returns 2. This discards fractional information; it does not round to the nearest integer or validate that the input was a whole number. When the fraction is part of the value we need, parseFloat can read it too:
var itemOne = parseFloat("2.99");
var itemTwo = 4;
var total = 1.08 * (itemOne + itemTwo);
alert(total);
To use parseFloat, pass in the text whose decimal numeric prefix we want to read. Both parseFloat and parseInt return Number values, so neither provides a separate exact-decimal type. They can also ignore trailing text, and a parsed result still needs to satisfy our input rules.
More Realistic Example Involving CSS Properties
Now that you've seen how to use parseInt and parseFloat using a pretty silly example, let's kick things up a few notches by looking at a situation where I had to use parseInt in real life!
Let's say you have an element with an id of mySquare whose width is set by the following style rule:
#mySquare {
width: 250px;
height: 250px;
background-color: green;
}
What I wanted to do is increase the width of this element by 10 pixels. To handle that, here is the code that I had initially:
var myElement = document.querySelector("#mySquare");
var style = window.getComputedStyle(myElement);
// getting 250 and removing the "px" part of the value
var initialWidth = style.getPropertyValue("width").split("px")[0];
// adding 10 and tagging "px" back
var newWidth = initialWidth + 10 + "px";
myElement.style.width = newWidth;
alert(newWidth);
Can you guess what the value for newWidth would be in the end?
The answer wasn't 260px as I would have wanted. Instead, the answer turned out to be 25010px instead. The problem lies in the following line:
var myElement = document.querySelector("#mySquare");
var style = window.getComputedStyle(myElement);
// getting 250 and removing the "px" part of the value
var initialWidth = style.getPropertyValue("width").split("px")[0];
// adding 10 and tagging "px" back
var newWidth = initialWidth + 10 + "px";
myElement.style.width = newWidth;
alert(newWidth);
The value of initialWidth at this point is 250...in the form of a string! When I attempt to add 10 to this value, what you get is the equivalent of combining two strings together: 25010. The solution is to wrap the initialWidth variable into a parseInt function before using it as part of my addition:
var myElement = document.querySelector("#mySquare");
var style = window.getComputedStyle(myElement);
// getting 250 and removing the "px" part of the value
var initialWidth = style.getPropertyValue("width").split("px")[0];
// adding 10 and tagging "px" back
var newWidth = parseInt(initialWidth) + 10 + "px";
myElement.style.width = newWidth;
alert(newWidth);
Once I did this, newWidth was the string "260px" I wanted. This fix uses our example's whole-pixel width. For fractional pixel widths, parseInt would lose the fraction; parseFloat can preserve it. Neither parser converts ems or percentages to pixels, so a general helper must establish the unit and handle nonnumeric values too. You can see another example of me using parseInt in an example where I display PHP data in a HTML page.
Wait, There's More!
If you thought the story with parseInt and parseFloat ended, then you are in for a surprise. These functions read numeric prefixes rather than evaluating arithmetic expressions or validating every character. Some arithmetic operators also convert strings differently from +. Here are a few examples:
// NaN
parseInt("foo");
// 200
parseInt("200 + 400 + 300");
// 200
parseInt("200abc")
// NaN
parseInt("abc200");
// "155" (a string)
"15" + 5;
// 3
"15" / 5;
// 75
"15" * 5;
// 4
parseInt("4.55");
Some of the results make sense. Some of the results are a bit strange...like dividing or multiplying stringified numbers ends up converting the string into a number first. That is different from what you see with the + operator.
The Number Function
Besides parseInt and parseFloat, you also have the Number function that you can use to turn a string into a number:
Number("3.14") // 3.14 as a number
For complete numeric text such as "3.14", Number can preserve the fractional value that parseInt would discard. Its string conversion requires the whole trimmed text to fit an accepted numeric form. A unit suffix such as px is not part of that form:
Number("20px") // NaN
For this example, both parseInt and parseFloat return the prefix 20. Number returns NaN, which is an actual Number value, not an absence of a result. Number also converts blank or whitespace-only text to 0, and it accepts the text "Infinity". Check required fields for blank text separately, and validate the converted result's finiteness and range when the field requires them. I often use parseFloat for known unit-bearing text, but the field's rules should decide which conversion is appropriate.
Conclusion
When we put a string and a number in the same room, the operator we choose matters. Decide what the text means, validate it, and establish the numeric value where it enters the calculation. If repeated conversion appears to be a performance problem, measure the actual workload before changing that boundary.
Thanks also to Axel Rauschmayer for reminding me about the Number function!
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇

