References

Beginner-friendly references for web development, with live, editable examples.

The JavaScript Number object

Object JavaScript All modern browsers Updated
Quick answer

JavaScript has a single Number type for all numbers, integers and decimals alike. Convert text to a number with Number(x), parseInt() or parseFloat(); format with toFixed(2); and validate with Number.isNaN() and Number.isInteger(). Beware floating-point rounding: 0.1 + 0.2 is 0.30000000000000004.

Overview

JavaScript keeps things simple with one numeric type, Number, covering both whole numbers and decimals (there's also BigInt for very large integers). You write numbers as literals (42, 3.14, 1e6), and the usual arithmetic operators work as expected.

The most-used helpers are about converting and formatting. Number("42"), parseInt("42px") and parseFloat("3.14") turn strings into numbers (the parse versions tolerate trailing text). (19.876).toFixed(2) formats to a fixed number of decimals and returns a string ("19.88"), which is perfect for money. For validation, Number.isNaN(x) safely detects the special NaN value and Number.isInteger(x) checks for whole numbers.

The one trap everybody meets is floating-point precision. Because numbers are stored in binary, 0.1 + 0.2 doesn't give exactly 0.3; it gives 0.30000000000000004. This isn't a JavaScript bug; it's how binary floating point works in every language. For display, round with Math.round() or toFixed(); for money, a common approach is to work in whole cents (integers) and divide only when displaying.

Syntax

Number("42");          // 42
parseInt("42px", 10);  // 42
parseFloat("3.14rem"); // 3.14
(19.876).toFixed(2);   // "19.88" (a string)
Number.isNaN(NaN);     // true
0.1 + 0.2;             // 0.30000000000000004

Example

Live example
<pre id="out" style="font:15px ui-monospace,monospace"></pre>
<script>
  const raw = '49.95 USD';

  const price = parseFloat(raw);
  const withTax = price * 1.2;

  document.getElementById('out').textContent =
    'parsed:  ' + price + '\n' +
    'w/ tax:  $' + withTax.toFixed(2) + '\n' +
    '0.1+0.2: ' + (0.1 + 0.2);
</script>

Best practices

  • Convert strings with Number(), or parseInt()/parseFloat() when there's trailing text.
  • Format for display with toFixed() (returns a string) and round with Math.round().
  • Check values with Number.isNaN() and Number.isInteger() rather than the global versions.
  • Avoid floating-point surprises for money by working in integer cents and dividing only to display.

Frequently asked questions

Why is 0.1 + 0.2 not 0.3 in JavaScript?
Numbers use binary floating point, which can't represent some decimals exactly, so the result is 0.30000000000000004. Round with toFixed() or Math.round() for display.
How do I convert a string to a number?
Use Number(str), or parseInt(str, 10) / parseFloat(str) when the string has trailing characters.
How do I round to 2 decimal places?
Use num.toFixed(2), which returns a string, or Math.round(num * 100) / 100 for a number.
How do I check for NaN?
Use Number.isNaN(value). It's reliable, unlike comparing with === NaN, which is always false.