JavaScript Math Object
Last Updated :
30 Jul, 2024
JavaScript Math object is used to perform mathematical operations on numbers. All the properties of Math are static and unlike other objects, it does not have a constructor.
We use Math only on Number data type and not on BigInt
Example 1: This example uses math object properties to return their values.
JavaScript
console.log("Math.LN10: " + Math.LN10);
console.log("Math.LOG2E: " + Math.LOG2E);
console.log("Math.Log10E: " + Math.LOG10E);
console.log("Math.SQRT2: " + Math.SQRT2);
console.log("Math.SQRT1_2: " + Math.SQRT1_2);
console.log("Math.LN2: " + Math.LN2);
console.log("Math.E: " + Math.E);
console.log("Math.PI: " + Math.PI);
OutputMath.LN10: 2.302585092994046
Math.LOG2E: 1.4426950408889634
Math.Log10E: 0.4342944819032518
Math.SQRT2: 1.4142135623730951
Math.SQRT1_2: 0.7071067811865476
Math.LN2: 0.6931471805599453
Math.E: 2.71828...
Example 2: Math object methods are used in this example.
JavaScript
console.log("Math.abs(-4.7): " + Math.abs(-4.7));
console.log("Math.ceil(4.4): " + Math.ceil(4.4));
console.log("Math.floor(4.7): " + Math.floor(4.7));
console.log("Math.sin(90 * Math.PI / 180): " +
Math.sin(90 * Math.PI / 180));
console.log("Math.min(0, 150, 30, 20, -8, -200): " +
Math.min(0, 150, 30, 20, -8, -200));
console.log("Math.random(): " + Math.random());
OutputMath.abs(-4.7): 4.7
Math.ceil(4.4): 5
Math.floor(4.7): 4
Math.sin(90 * Math.PI / 180): 1
Math.min(0, 150, 30, 20, -8, -200): -200
Math.random(): 0.7416861489868538
Supported Browsers:
- Chrome
- Edge
- Firefox
- Opera
- Safari
We have a complete list of JavaScript Math Object methods, to check those please go through the JavaScript Math Complete Reference article
JavaScript Math Object – FAQs
What is the Math object in JavaScript?
The Math object is a built-in object that provides properties and methods for mathematical constants and functions. It is not a constructor, so all its properties and methods are static and can be called without creating a Math object instance.
How do you use the Math object?
You use the Math object by calling its properties and methods directly. For example, Math.PI for the value of π or Math.sqrt() for calculating the square root.
How do you generate random numbers using the Math object?
You can generate random numbers using Math.random(), which returns a floating-point number between 0 (inclusive) and 1 (exclusive). To generate a random number within a specific range, you can scale and shift the result.
How do you find the maximum or minimum of a set of numbers?
You can find the maximum or minimum of a set of numbers using Math.max() and Math.min() respectively. Both methods accept zero or more arguments.