String to Number in JavaScript: parseInt, parseFloat and Number

Oct 18, 2026 · 3 min read · Developer

Data from forms, files and APIs usually arrives as text. Even if a user types 25, your code receives the characters "2" and "5". To do maths, you must convert the text to a number. JavaScript gives you a few ways, and they do not behave the same.

The main options

What is NaN?

NaN means "not a number". It is what you get when the conversion fails. A funny fact: NaN is not equal to anything, even itself. Use Number.isNaN(value) to check for it.

Empty text is a trap

Number("") gives 0, but parseInt("") gives NaN. If a user leaves a box empty and you treat it as zero, you may save wrong data. Always check for empty text first.

Always give parseInt a radix

Write parseInt(text, 10). The second number tells JavaScript to read the text as normal decimal. Without it, text like "0x1F" is read as hexadecimal, which can surprise you.

Which one should I use?

Very big numbers

JavaScript numbers lose accuracy above 9,007,199,254,740,991. Long IDs, like card numbers or social media post IDs, should stay as text, or you can use BigInt.

Try it yourself

Instead of guessing, paste your values into the String to Number tool on our Developer Converters page. It shows parseInt, parseFloat, Number and the plus sign side by side, one line per value, so you can see which method suits your data.

Frequently asked questions

What is the difference between parseInt and Number?

parseInt reads digits from the start of the text and stops at the first non-digit, so parseInt("12px") is 12. Number needs the whole text to be a valid number, so Number("12px") is NaN. Use Number when you want strict checking.

How do I check if a value is a valid number?

Convert it with Number(text) and then use Number.isFinite(result). That rejects NaN, Infinity and empty text that was turned into 0, if you also check that the text was not blank.

Why does 0.1 + 0.2 not equal 0.3?

JavaScript stores numbers in binary, and 0.1 cannot be written exactly in binary, just as one third cannot be written exactly in decimal. Round results for display with toFixed, or work in whole units such as paise for money.

How do I convert a number with commas, like 1,250?

Remove the commas first: Number("1,250".replace(/,/g, "")). Be careful with numbers written in other countries' styles, where a comma can be the decimal separator.

Try it now

Open the free Developer Converters. It runs in your browser, so nothing is uploaded.