JavaScript Math
Perform mathematical operations and calculations
🔢 What is JavaScript Math?
The Math object provides mathematical constants and functions. You can perform calculations, generate random numbers, and use mathematical operations.
// Basic math operations
console.log(Math.round(4.7)); // 5
console.log(Math.random()); // Random number between 0 and 1
Output:
5
0.7834592847362
Key Math Concepts
Rounding
Round numbers up, down, or nearest
Math.round(4.7); // 5
Random Numbers
Generate random values
Math.random(); // 0 to 1
Min/Max
Find smallest or largest values
Math.max(1, 5, 3); // 5
Power/Square
Calculate powers and square roots
Math.pow(2, 3); // 8
🔹 Rounding Numbers
Different ways to round numbers:
let number = 4.7;
console.log("Original:", number); // 4.7
console.log("Round:", Math.round(number)); // 5 (nearest integer)
console.log("Floor:", Math.floor(number)); // 4 (round down)
console.log("Ceil:", Math.ceil(number)); // 5 (round up)
// More examples
console.log(Math.round(4.4)); // 4
console.log(Math.round(4.5)); // 5
console.log(Math.floor(4.9)); // 4
console.log(Math.ceil(4.1)); // 5
Output:
Original: 4.7
Round: 5
Floor: 4
Ceil: 5
4
5
4
5
🔹 Random Numbers
Generate random numbers for games and applications:
// Random decimal between 0 and 1
console.log(Math.random()); // 0.7834592847362
// Random number between 0 and 10
console.log(Math.random() * 10); // 7.834592847362
// Random integer between 1 and 10
console.log(Math.floor(Math.random() * 10) + 1); // 8
// Random integer between 1 and 6 (dice roll)
let diceRoll = Math.floor(Math.random() * 6) + 1;
console.log("Dice roll:", diceRoll); // 4
// Function to get random number between min and max
function randomBetween(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(randomBetween(5, 15)); // Random number between 5 and 15
Output:
0.7834592847362
7.834592847362
8
Dice roll: 4
12
🔹 Min, Max, and Absolute
Find minimum, maximum, and absolute values:
// Find minimum and maximum
console.log(Math.min(5, 10, 2, 8)); // 2
console.log(Math.max(5, 10, 2, 8)); // 10
// With arrays (using spread operator)
let numbers = [5, 10, 2, 8, 15];
console.log(Math.min(...numbers)); // 2
console.log(Math.max(...numbers)); // 15
// Absolute value (distance from zero)
console.log(Math.abs(-5)); // 5
console.log(Math.abs(5)); // 5
console.log(Math.abs(-3.14)); // 3.14
Output:
2
10
2
15
5
5
3.14
🔹 Powers and Square Roots
Calculate powers, square roots, and more:
// Power (base, exponent)
console.log(Math.pow(2, 3)); // 8 (2 to the power of 3)
console.log(Math.pow(5, 2)); // 25 (5 squared)
console.log(Math.pow(10, 3)); // 1000 (10 cubed)
// Square root
console.log(Math.sqrt(16)); // 4
console.log(Math.sqrt(25)); // 5
console.log(Math.sqrt(2)); // 1.4142135623730951
// Math constants
console.log(Math.PI); // 3.141592653589793
console.log(Math.E); // 2.718281828459045
// Calculate circle area
let radius = 5;
let area = Math.PI * Math.pow(radius, 2);
console.log("Circle area:", area); // 78.53981633974483
Output:
8
25
1000
4
5
1.4142135623730951
3.141592653589793
2.718281828459045
Circle area: 78.53981633974483