Math.PI
Math.random()
Math.round(4.6)
console.log(Math.PI); // 3.141592653589793
Used in:
Rounds a number to the nearest integer.
Math.round(4.5); // 5
Math.round(4.4); // 4
Rounds up to the nearest integer.
Math.ceil(4.1); // 5
Math.min(3, 5, 1, 8); // 1
Math.max(3, 5, 1, 8); // 8
Used in:
Returns a random number between 0 and 1.
Math.floor(Math.random() * 10) + 1;
Real-world use cases:
Math.pow(2, 3); // 8
Math.sqrt(25); // 5
2 ** 3; // 8
Math.abs(-10); // 10
Math.sqrt(25); // 5
Math.sqrt(25); // 5
2 ** 3; // 8
Math.abs(-10); // 10
Math.sin(angle);
Math.cos(angle);
Math.tan(angle);
Math.sin(Math.PI / 2); // 1
Used for:
function calculateDiscount(price, discountPercent) {
const discount = (price * discountPercent) / 100;
return Math.round(price - discount);
}
calculateDiscount(999, 15); // 849
Use the JavaScript Math API when: