JavaScript Numbers
The single number type, arithmetic operators, floating-point pitfalls, and the Math object.
What you'll learn
- Why JavaScript uses one number type for integers and decimals
- Arithmetic operators, modulo, and exponentiation (**)
- Floating-point precision issues and how to work around them
- The Math object: round, floor, ceil, random, max, min, abs
- Converting strings to numbers with Number(), parseInt(), parseFloat()
Concept
One Number Type
Unlike many languages that separate integers and floats, JavaScript has a single number type based on the IEEE 754 double-precision format. The value 42, 3.14, and -0 are all the same type. For arbitrarily large integers, ES2020 added the bigint type (written with an n suffix, e.g. 9007199254740993n).
Arithmetic Operators
JavaScript supports the standard operators: +, -, *, /, % (modulo), and (exponentiation). Operator precedence follows math conventions: multiplication and division bind tighter than addition. The + operator is overloaded — it adds numbers but concatenates** strings, which is a common source of bugs when mixing types.
Floating-Point Precision
Because numbers are binary floating-point, some decimal values cannot be represented exactly:
0.1 + 0.2 === 0.3 // false — it is 0.30000000000000004
To compare floats, check that the absolute difference is below a small epsilon. For money, work in integer cents or use a dedicated decimal library. Rounding with Math.round() before display also hides the noise.
The Math Object
Math is a built-in object with constants and functions: Math.round(), Math.floor(), Math.ceil(), Math.abs(), Math.max(), Math.min(), Math.pow(), Math.sqrt(), and Math.random() (a float between 0 and 1). To get a random integer in a range, combine Math.floor() with Math.random().
Converting Strings to Numbers
Number(str) parses a full string to a number (returns NaN if invalid). parseInt(str, 10) and parseFloat(str) parse from the start and stop at the first invalid character. Always pass the radix (10) to parseInt to avoid legacy octal surprises. Check for NaN with Number.isNaN(), not the global isNaN() which coerces its argument.
Example
// Floating-point precision pitfall
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // false
// Safe float comparison with epsilon
function approxEqual(a, b, eps = 1e-9) {
return Math.abs(a - b) < eps;
}
console.log(approxEqual(0.1 + 0.2, 0.3)); // true
// The Math object
const prices = [19.99, 5.5, 42, 8.75];
console.log("Max:", Math.max(...prices));
console.log("Min:", Math.min(...prices));
console.log("Rounded:", Math.round(5.5)); // 6
// Random integer in a range [min, max]
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log("Dice roll:", randomInt(1, 6));
// Converting strings to numbers
console.log(Number("42")); // 42
console.log(parseInt("42px", 10)); // 42
console.log(parseFloat("3.14abc")); // 3.14
console.log(Number.isNaN(Number("abc"))); // true
Shows the classic 0.1 + 0.2 precision issue, an epsilon-based comparison, Math methods for min/max/round, a random dice roll helper, and the three string-to-number conversion functions.
Try it
- Math Evaluator
Evaluate arithmetic expressions and see operator precedence in action.
- JSON Formatter
Inspect numeric values in JSON data structures.
Common mistakes
The mistake
Expecting 0.1 + 0.2 to equal 0.3 exactly
The fix
Floating-point math cannot represent some decimals exactly. Compare with an epsilon (Math.abs(a - b) < 1e-9) or work in integer units (e.g. cents) for money.
The mistake
Using parseInt without a radix argument
The fix
Always pass the radix: parseInt(str, 10). Without it, strings starting with '0' could be parsed as octal in older environments. Number(str) is safer for full-string parsing.
Related Resources
Related Cheatsheets
- JavaScript Cheatsheet
Number methods, Math reference, and conversion functions.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.