Numbers in JavaScript
JavaScript handles numbers in a simple but powerful way. Whether you’re working with whole numbers, decimals, or special values like NaN and Infinity, this guide will help you understand how numbers work and how to use them effectively in your code.
📚 Table of Contents
- Types of Numbers
- Writing Numbers (Literals)
- Useful Number Methods
- Special Values: NaN, Infinity
- Converting Strings to Numbers
- The Math Object
- Precision Issues
- Best Practices
🧮 Types of Numbers
In JavaScript, all regular numbers—whether integers (like 10) or decimals (like 3.14)—are stored as the same type: a 64-bit floating-point number (based on the IEEE 754 standard).
typeof 42; // "number"typeof 3.14; // "number"BigInt
For working with really large integers beyond the safe limit, use BigInt (introduced in ES2020):
typeof 12345678901234567890n; // "bigint"🔢 Writing Numbers (Literals)
You can write numbers in several formats:
- Decimal:
42,3.14 - Binary:
0b1010→ 10 - Octal:
0o52→ 42 - Hexadecimal:
0x2A→ 42 - Exponential Notation:
1.5e4→ 15000
🛠️ Useful Number Methods
Here are some helpful methods built into JavaScript for working with numbers:
Number.isNaN(value); // Checks if value is NaNNumber.isFinite(value); // Checks if value is a finite numberNumber.parseInt(str, 10); // Parses a string to an integerNumber.parseFloat(str); // Parses a string to a float
let n = 12.3456;n.toFixed(2); // "12.35" – keeps 2 decimal placesn.toPrecision(4); // "12.35" – total length of the number⚠️ Special Values: NaN, Infinity
JavaScript also includes some special numeric values:
NaN→ “Not-a-Number”, used when a calculation doesn’t make sense.Infinityand-Infinity→ Result of dividing by zero or very large/small values.
Examples:
0 / 0; // NaN1 / 0; // Infinity-1 / 0; // -Infinity🔄 Converting Strings to Numbers
You can turn strings into numbers using:
Number("123"); // 123parseInt("101", 2); // 5 (binary to decimal)parseFloat("3.14"); // 3.14Tip: Prefer parseInt or parseFloat when parsing input strings.
🧠 The Math Object
JavaScript includes a built-in Math object with many useful functions:
Math.round(4.7); // 5Math.floor(4.7); // 4Math.ceil(4.1); // 5Math.max(3, 9, 2); // 9Math.min(3, 9, 2); // 2Math.random(); // Random number between 0 and 1Math.pow(2, 3); // 8 (2^3)Math.sqrt(16); // 4It also includes trigonometry, logarithms, and more.
⚖️ Precision Issues
Because JavaScript uses floating-point math, some results can be a bit off:
0.1 + 0.2 === 0.3; // false 😬To safely compare decimals, use a small tolerance value:
Math.abs((0.1 + 0.2) - 0.3) < Number.EPSILON; // true ✅✅ Best Practices
- Use
Number.isNaN()andNumber.isFinite()instead of the globalisNaN()orisFinite()functions. - Use
parseInt()andparseFloat()for parsing user input. - Watch out for floating-point rounding errors.
- Use
BigIntwhen working with very large numbers that might lose precision.
💡 Final Thoughts
Numbers are everywhere in JavaScript—from calculations and user input to complex data structures. Knowing how to use them properly helps you write reliable and accurate code.
📖 For more in-depth info, check out the MDN docs on Numbers.