JavaScript uses the Number type to represent both integer and floating-point numbers. It supports standard arithmetic operations and provides a built-in Math object for advanced calculations.
Basic Number Operations
+Addition-Subtraction*Multiplication/Division%Modulus (remainder)**Exponentiation (power)
📌 Deep Dive: Simple Arithmetic
let a = 10;
let b = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.3333333333333335
console.log(a % b); // 1
console.log(a ** b); // 1000
7
30
3.3333333333333335
1
1000
Important Number Properties
Infinityand-Infinityrepresent overflow values.NaNmeans "Not a Number" (invalid number result).Number.MAX_SAFE_INTEGERis the largest safe integer (253 - 1).

💡 Floating-Point Precision
Decimal calculations can sometimes be imprecise due to binary representation. For example, 0.1 + 0.2 !== 0.3.
The Math Object
The Math object provides useful constants and functions for advanced math:
| Property / Method | Description |
|---|---|
Math.PI | The value of π (~3.14159) |
Math.abs(x) | Absolute value of x |
Math.round(x) | Rounds x to nearest integer |
Math.floor(x) | Rounds x down |
Math.ceil(x) | Rounds x up |
Math.sqrt(x) | Square root of x |
Math.pow(x, y) | x raised to the power y |
Math.random() | Random number between 0 (inclusive) and 1 (exclusive) |
📌 Deep Dive: Using Math Methods
console.log(Math.PI); // 3.141592653589793
console.log(Math.abs(-5)); // 5
console.log(Math.round(4.7)); // 5
console.log(Math.floor(4.7)); // 4
console.log(Math.ceil(4.3)); // 5
console.log(Math.sqrt(16)); // 4
console.log(Math.pow(2, 3)); // 8
console.log(Math.random()); // e.g. 0.721345...
5
5
4
5
4
8
0.721345...
⚠️ Beware of Floating Point Arithmetic
Use methods like toFixed() or rounding to handle precision issues in decimal numbers.
Converting to Numbers
You can convert values to numbers using:
Number(value)- converts to a number orNaNif invalidparseInt(string, radix)- parses an integer from a stringparseFloat(string)- parses a floating-point number from a string
📌 Deep Dive: Number Conversion
console.log(Number("123")); // 123
console.log(Number("123abc")); // NaN
console.log(parseInt("123abc")); // 123
console.log(parseInt("0xF", 16)); // 15
console.log(parseFloat("3.14abc"));// 3.14
NaN
123
15
3.14
💡 Type Coercion in Arithmetic
JavaScript automatically converts strings to numbers in math operations when possible, but concatenates if + is used with a string.
Summary
- Numbers in JS are all floating-point (64-bit IEEE 754).
- Use arithmetic operators for basic calculations.
- The
Mathobject offers many useful math functions. - Be cautious with floating-point precision and type coercion.
Quick Knowledge Check
Test what you just learned
Question 1 of 2
What does 5 % 2 return?
Question 2 of 2
Which method rounds a number down to the nearest integer?
Loading results...