The Math object in JavaScript provides built-in properties (constants) and methods (functions) for common mathematical operations such as rounding, powers, square roots, absolute values, and random number generation using Math.random(). It is a static utility object, so you use it directly as Math.something, not by creating instances.
Math is a built-in static object; you never use new Math().Math.PI, Math.E, Math.SQRT2) are constants, not functions.Math.round(), Math.floor(), Math.sqrt(), Math.abs()) perform numeric operations.Math.random() returns a pseudo-random number in the range [0, 1) (0 inclusive, 1 exclusive).Math.random() with Math.floor() to produce random integers in any range you want.The general pattern for using the Math object is:
result = Math.methodName(value);
Math.PI, Math.E, Math.SQRT2.Math.round(4.6), Math.sqrt(16).Math is not a constructor. This is invalid: new Math().A common pattern for generating a random integer between two values is:
Math.floor(Math.random() * (max - min + 1)) + min
// Math properties (constants)
console.log(Math.PI); // 3.141592653589793
console.log(Math.E); // 2.718281828459045
console.log(Math.SQRT2); // 1.4142135623730951
console.log(Math.LN2); // 0.6931471805599453
// Math methods
console.log(Math.round(4.6)); // 5 (nearest integer)
console.log(Math.ceil(4.1)); // 5 (round up)
console.log(Math.floor(4.9)); // 4 (round down)
console.log(Math.trunc(4.9)); // 4 (remove decimals)
console.log(Math.pow(2, 3)); // 8 (2 to the power of 3)
console.log(Math.sqrt(16)); // 4 (square root)
console.log(Math.abs(-7)); // 7 (absolute value)
console.log(Math.max(1, 5, 9)); // 9 (largest value)
console.log(Math.min(1, 5, 9)); // 1 (smallest value)
// A random number between 0 (inclusive) and 1 (exclusive)
console.log(Math.random()); // e.g., 0.47283478
// Random integer between 0 and 9
const random0to9 = Math.floor(Math.random() * 10);
console.log(random0to9);
// Random integer between 1 and 100
const random1to100 = Math.floor(Math.random() * 100) + 1;
console.log(random1to100);
// Helper: random integer between min and max (inclusive)
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
console.log(getRandomInt(1, 6)); // e.g., 4 (like a dice roll)
console.log(getRandomInt(50, 100)); // e.g., 73
// Random item from an array
const colors = ["Red", "Green", "Blue"];
const randomIndex = Math.floor(Math.random() * colors.length);
const randomColor = colors[randomIndex];
console.log(randomColor);
// Dice roll (1 to 6)
const dice = Math.floor(Math.random() * 6) + 1;
console.log("Rolled:", dice);
Math.PI, Math.E, Math.SQRT2, and Math.LN2 give you commonly used mathematical constants.Math.round(), Math.floor(), Math.ceil(), and Math.trunc() help you control how decimal numbers are converted to integers.Math.max() and Math.min() find the largest and smallest values from a list of numbers.Math.random() on its own returns a decimal between 0 and 1. Multiplying it and using Math.floor() lets you create integer ranges.getRandomInt(min, max) function wraps this pattern into a reusable piece of code for any integer range.colors.length is used to pick a random color from the list.Click the button to roll a dice (values from 1 to 6) using Math.random() and Math.floor().
Current roll: -
Math is not a constructor; never use new Math().Math.random() together with Math.floor() (or Math.trunc()) to generate random integers.getRandomInt(min, max) instead of repeating the same random-range logic everywhere.Math.abs() to remove negative signs when you only care about magnitudes.Math.random() is pseudo-random and not suitable for cryptographic purposes.getRandomInt() or by writing the expression yourself.3.14159 using Math.round(), Math.floor(), and Math.ceil() and compare the results.Math.max() and Math.min() to find the largest and smallest values from an array of numbers.colors array example.