JavaScript — Numbers
What are Numbers?
JavaScript has one number type that covers both integers and decimals:
const age = 25; // integer
const price = 9.99; // decimal
const negative = -10; // negative
const scientific = 1e6; // 1,000,000 (scientific notation)
Number Methods
const num = 42;
// Conversion
Number("42"); // 42
Number("hello"); // NaN
parseInt("42px"); // 42
parseFloat("3.14"); // 3.14
num.toString(); // "42"
// Checking
Number.isInteger(42); // true
Number.isInteger(3.14); // false
Number.isNaN(NaN); // true
Number.isFinite(42); // true
Number.isFinite(Infinity); // false
// Formatting
(3.14159).toFixed(2); // "3.14"
(3.14159).toPrecision(3); // "3.14"
(1000000).toLocaleString(); // "1,000,000"
Math Operations
// Basic arithmetic
5 + 3; // 8 (addition)
5 - 3; // 2 (subtraction)
5 * 3; // 15 (multiplication)
5 / 3; // 1.666... (division)
5 % 3; // 2 (modulus/remainder)
5 ** 3; // 125 (exponentiation)
Math Object
Math.PI; // 3.141592653589793
Math.E; // 2.718281828459045
Math.round(4.7); // 5
Math.ceil(4.3); // 5
Math.floor(4.7); // 4
Math.abs(-5); // 5
Math.max(1, 5, 3); // 5
Math.min(1, 5, 3); // 1
Math.sqrt(16); // 4
Math.pow(2, 3); // 8
Math.random(); // 0 to 1 (exclusive)
Math.floor(Math.random() * 10) + 1; // 1 to 10
Floating Point Quirks
0.1 + 0.2; // 0.30000000000000004 (not 0.3!)
0.1 + 0.2 === 0.3; // false
// Fix: compare with tolerance
Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON; // true
// Or round before comparing
(0.1 + 0.2).toFixed(1) === "0.3"; // true
Number Conversion
// String to number
+"42"; // 42
Number("42"); // 42
parseInt("42px"); // 42 (ignores trailing text)
parseFloat("3.14em"); // 3.14
// Number to string
String(42); // "42"
(42).toString(); // "42"
(42).toFixed(2); // "42.00"
// Check validity
Number.isFinite("hello"); // false
Number.isNaN(NaN); // true
BigInt
For very large integers beyond Number.MAX_SAFE_INTEGER:
const big = 9007199254740991n; // note the 'n'
const alsoBig = BigInt(9007199254740991);
// Can't mix BigInt and Number
// 42n + 1; // TypeError
42n + 1n; // 43n
Common Patterns
// Random integer between min and max
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Clamp a number between bounds
function clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
// Check if even or odd
const isEven = n => n % 2 === 0;
// Round to N decimal places
function roundTo(num, decimals) {
return Math.round(num * 10 ** decimals) / 10 ** decimals;
}
roundTo(3.14159, 2); // 3.14
Mini Practice
- Calculate the area of a circle using
Math.PI - Generate a random number between 1 and 100
- Round a decimal to 2 places using
toFixed - Write a function that returns the larger of two numbers
- Convert a string price to a formatted currency string
Up Next
Next: Operators →
Related Topics
Frequently Asked Questions about Numbers
What is Numbers in JavaScript?
Numbers is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn Numbers?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Numbers.
Why is Numbers important in JavaScript?
Numbers is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.