In JavaScript, mathematical functions are provided as static methods on the built-in Math object. Because it is a static namespace rather than a constructor, you call these functions directly as Math.methodName() without instantiating it.
- We use Math only on Number data type and not on BigInt
The Math Object
The JavaScript Math object allows you to perform mathematical tasks.
The Math object is static.
All methods and properties can be used without creating a Math object first.
Math Properties (Constants)
The syntax for any Math property is : Math.property.
JavaScript provides 8 mathematical constants that can be accessed as Math properties:
This example uses math object properties to return their values.
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
Output
Math.E: 2.718281828459045
Math.PI: 3.141592653589793
Math.SQRT2: 1.4142135623730951
Math.SQRT1_2: 0.7071067811865476
Math.LN2: 0.6931471805599453
Math.LN10: 2.302585092994046
Math.LOG2E: 1.4426950408889634
Math.Log10E: 0.4342944819032518
Math Constants
Constants are fixed values provided by the Math object.
1. Math.PI
Returns the value of π (Pi).
Definition: The ratio of a circle's circumference to its diameter.
console.log(Math.PI);
Output
3.141592653589793
Example
let radius = 5;
let area = Math.PI * radius * radius;
console.log(area);
Output
78.53981633974483
2. Math.E
Returns Euler's number.
Definition: The base of natural logarithms.
console.log(Math.E);
Output
2.718281828459045
3. Math.SQRT2
Returns the square root of 2.
console.log(Math.SQRT2);
Output
1.4142135623730951
4. Math.SQRT1_2
Returns the square root of 1/2.
console.log(Math.SQRT1_2);
Output
0.7071067811865476
5. Math.LN2
Returns the natural logarithm of 2.
console.log(Math.LN2);
Output
0.6931471805599453
Example
To verify that ln(2) equals Math.LN2:
console.log(Math.log(2));
console.log(Math.LN2);
Output
0.6931471805599453
0.6931471805599453
6. Math.LN10
Returns the natural logarithm of 10.
console.log(Math.LN10);
Output
2.302585092994046
Example
console.log(Math.log(10));
console.log(Math.LN10);
Output
2.302585092994046
2.302585092994046
7. Math.LOG2E
Returns the base-2 logarithm of E.
console.log(Math.LOG2E);
Output
1.4426950408889634
8. Math.LOG10E
Returns the base-10 logarithm of E.
console.log(Math.LOG10E);
Output
0.4342944819032518
Math Methods
The syntax for Math any methods is : Math.method(number)
1. Math.abs()
Returns the absolute (positive) value of a number.
console.log(Math.abs(-25));
Output
25
2. Math.round()
Rounds a number to the nearest integer.
console.log(Math.round(4.6)); // 5
console.log(Math.round(4.4)); // 4
3. Math.ceil()
Rounds a number upward to the nearest integer.
console.log(Math.ceil(4.1));
Output
5
4. Math.floor()
Rounds a number downward to the nearest integer.
console.log(Math.floor(4.9));
Output
4
5. Math.trunc()
Removes the decimal part and returns only the integer portion.
console.log(Math.trunc(4.99));
Output
4
6. Math.random()
Returns a random number between 0 (inclusive) and 1 (exclusive).
console.log(Math.random());
Output
0.735621
Generate Random Number Between 0 and 9
let num = Math.floor(Math.random() * 10);
console.log(num);
Generate Random number from 1 to 10
let num = Math.floor(Math.random() * 10) + 1;
console.log(num);
7. Math.max()
Returns the largest value from a list of numbers.
console.log(Math.max(10, 20, 30, 40));
Output
40
8. Math.min()
Returns the smallest value from a list of numbers.
console.log(Math.min(10, 20, 30, 40));
Output
10
9. Math.pow()
Raises a number to a specified power.
console.log(Math.pow(2, 3));
Output
8
Modern Alternative
console.log(2 ** 3);
10. Math.sqrt()
Returns the square root of a number.
console.log(Math.sqrt(64));
Output
8
11. Math.cbrt()
Returns the cube root of a number.
console.log(Math.cbrt(27));
Output
3
12. Math.sign()
Returns whether a number is positive, negative, or zero.
console.log(Math.sign(20)); // 1
console.log(Math.sign(-20)); // -1
console.log(Math.sign(0)); // 0
13. Math.exp()
Returns e raised to the given power.
console.log(Math.exp(2));
Output
7.38905609893065
Example
console.log(Math.exp(1)); // same as Math.E
14. Math.log()
Returns the natural logarithm (base e).
console.log(Math.log(Math.E));
Output
1
15. Math.log2()
Returns the base-2 logarithm.
console.log(Math.log2(8));
Output
3
Because
2 ** 3 = 8
16. Math.log10()
Returns the base-10 logarithm.
console.log(Math.log10(1000));
Output
3
Because
10 ** 3 = 1000
17. Math.sin()
Returns the sine of an angle (in radians).
console.log(Math.sin(Math.PI / 2));
Output
1
18. Math.cos()
Returns the cosine of an angle (in radians).
console.log(Math.cos(0));
Output
1
19. Math.tan()
Returns the tangent of an angle (in radians).
console.log(Math.tan(Math.PI / 4));
Output
1
20. Math.hypot()
- Returns the square root of the sum of squares of its arguments.
- Returns the hypotenuse length using the Pythagorean theorem.
console.log(Math.hypot(3, 4));
Output
5
Example
let width = 6;
let height = 8;
console.log(Math.hypot(width, height));
Output
10
These are all some most frequently used methods:
Math.random(); //Generate random values
Math.floor(); //Round down
Math.ceil(); //Round up
Math.round(); //Nearest integer
Math.max(); //Find largest value
Math.min(); //Find smallest value
Math.abs(); //Get positive value
Math.sqrt(); //Square root
Math.pow(); //Exponents
Math.trunc(); //Remove decimals
These are the methods you'll use most often in real-world JavaScript projects.
Reference
https://www.w3schools.com/jS/js_math.asp
https://www.geeksforgeeks.org/javascript/javascript-math-object/
https://www.programiz.com/javascript/library/math
Top comments (0)