What are some commonly used Math object methods?
Short answer
Math.round(), Math.floor(), and Math.ceil() handle rounding; Math.max()/Math.min() find extremes among arguments; Math.random() generates a pseudo-random float between 0 (inclusive) and 1 (exclusive); Math.abs() returns absolute value.
Advertisement
AModel answer
Math.round(), Math.floor(), and Math.ceil() handle rounding; Math.max()/Math.min() find extremes among arguments; Math.random() generates a pseudo-random float between 0 (inclusive) and 1 (exclusive); Math.abs() returns absolute value.
Math.round(4.5); // 5
Math.floor(4.9); // 4
Math.random(); // e.g. 0.5834...
Math.max(3, 7, 2); // 7Real-world example
Generating a random integer within a specific range for a dice-roll or ID-suffix generator.
Likely follow-ups
- How do you generate a random integer between two specific values using Math.random()?
Related topics