The Math object in Javascript provides a set of properties and methods for performing mathematical operations.
It is not a constructor and cannot be instantiated, so you use its properties and methods directly.
Represents the mathematical constant π (pi) value.
console.log(Math.PI); // 3.141592
Represents the mathematical constant e (Euler's number).
console.log(Math.E); // 2.718281828459045
console.log(Math.abs(10)); // Output: 10 console.log(Math.abs(-10)); // Output: 10
console.log(Math.round(9.44)); // Output: 9 console.log(Math.round(9.96)); // Output: 10
console.log(Math.floor(6.9)); // Output: 6
console.log(Math.ceil(6.1)); // Output: 7 console.log(Math.ceil(7.8)); // Output: 8
Math.abs(x): Returns the absolute value of x.
Math.round(x): Rounds x to the nearest integer.
Math.floor(x): Rounds x down to the nearest integer.
Math.ceil(x): Rounds x up to the nearest integer.
console.log(Math.pow(3, 3)); // Output: 27
console.log(Math.sqrt(4)); // Output: 2
console.log(Math.exp(1)); // Output: 2.718281828459045
console.log(Math.log(Math.E)); // Output: 1
console.log(Math.log10(100)); // Output: 2
Math.pow(x, y): Returns x raised to the power of y.
Math.sqrt(x): Returns the square root of x.
Math.exp(x): Returns e raised to the power of x.
Math.log(x): Returns the natural logarithm (base e) of x.
Math.log10(x): Returns the base 10 logarithm of x.
Math.random(): Returns a random floating-point number between 0 (inclusive) and 1 (exclusive).
console.log(Math.random()); // Random number between 0 and 1
console.log(Math.min(3, 7, 10, 5)); // Output: 3
console.log(Math.max(3, 7, 10, 5)); // Output: 10
// Random integer between 0 and 10 let randomInteger = Math.floor(Math.random() * 10); console.log(randomInteger) // Output: Rnadmonly generated value is 8
Math.min(x, y, ...args): Returns the smallest of the provided numbers.
Math.max(x, y, ...args): Returns the largest of the provided numbers.
Math.floor(Math.random()): Generates a random integer between 0 and 1 (inclusive).