JavaScript parseInt(), parseFloat(), and Number() Compared
parseInt() reads an integer prefix, parseFloat() reads a decimal number prefix, and Number() converts the entire string. For '12.5px', the respective results are 12, 12.5, and NaN.
Read an integer or decimal prefix
parseInt() reads digits in the specified base. Use 10 for decimal or 16 for hexadecimal such as ff. parseFloat() accepts a decimal point and a valid exponent in the prefix. It does not validate a trailing unit. ECMAScript · parseInt() ECMAScript · parseFloat()
console.log(parseInt('12.5px', 10)); // 12
console.log(parseFloat('12.5px')); // 12.5
console.log(parseInt('ff', 16)); // 255
Convert the full string with Number()
Number() allows surrounding whitespace and converts empty or whitespace-only strings to 0. It also accepts forms such as 1e3 and 0x10. If the full conversion fails, the result is NaN. ECMAScript · StringToNumber
| Input | parseInt(…, 10) | parseFloat() | Number() |
|---|---|---|---|
"12.5px" |
12 | 12.5 | NaN |
"12.5" |
12 | 12.5 | 12.5 |
"" |
NaN | NaN | 0 |
"1e3" |
1 | 1000 | 1000 |
"0x10" |
0 | 0 | 16 |
Define the numeric format a form accepts
If a form expects decimal input with an optional sign and point, check the format first. This example accepts -12.5 and .5 and rejects units, exponents, commas, and a final point with no digit after it. After conversion, Number.isFinite() excludes infinity.
function readDecimal(text) {
const trimmed = text.trim();
if (!/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$/.test(trimmed)) {
return null;
}
const value = Number(trimmed);
return Number.isFinite(value) ? value : null;
}
Compare four outputs in a complete example
Save the code as parse-number.html in UTF-8 and open it in a browser. The default value 12.5px gives 12.5, 12, and NaN in the three conversions. The last line shows whether the separate decimal check accepts it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare number conversions</title>
</head>
<body>
<label for="value">String to convert</label>
<input id="value" value="12.5px">
<button id="compare" type="button">Compare</button>
<pre id="result" aria-live="polite"></pre>
<script>
const input = document.querySelector('#value');
const result = document.querySelector('#result');
function readDecimal(text) {
const trimmed = text.trim();
if (!/^[+-]?(?:\d+(?:\.\d+)?|\.\d+)$/.test(trimmed)) {
return null;
}
const value = Number(trimmed);
return Number.isFinite(value) ? value : null;
}
const rejected = "Input rejected";
function compare() {
const text = input.value;
const decimal = readDecimal(text);
result.textContent = [
`parseInt(10): ${parseInt(text, 10)}`,
`parseFloat: ${parseFloat(text)}`,
`Number: ${Number(text)}`,
`Decimal check: ${decimal ?? rejected}`
].join('\n');
}
document.querySelector('#compare').addEventListener('click', compare);
compare();
</script>
</body>
</html>
Clear the field to see Number() return 0 while the decimal check rejects the input. Enter 1e3 to see 1,000 from parseFloat() and Number() and 1 from decimal parseInt().




