Javascript Math Object
Written By: Avinash Malhotra
Updated on
JavaScript Math Object is a build-in Object used to get Maths Properties (constants) and methods like PI, sinθ, random number, Square root, power etc.
Math Object
JS Math Object is a build in JavaScript Object to get Mathematical properties, Mathematical constants and Maths methods.
const m=Math; // return math object
const pi=m.PI; // return value of pie
Math is not a constructor object. Do not use Math Object with new operator. This will raise syntax error in console.
Math Object List
Mathematical Constants
JavaScript supports Eight Properties to get Maths Constants
Property | Value | Meaning |
---|---|---|
Math.PI | 3.141592653589793 | value of PI |
Math.SQRT2 | 1.4142135623730951 | square root of 2 |
Math.SQRT1_2 | 0.7071067811865476 | reciprocal of square root of 2, i.e, 1 divided by square root of 2 |
Math.E | 2.718281828459045 | Euler's Constant |
Math.LN2 | 0.6931471805599453 | Natural logarithm of 2 |
Math.LN10 | 2.302585092994046 | Natural logarithm of 10 |
Math.LOG2E | 1.4426950408889634 | Log base 2 of Euler's Constant |
Math.LOG10E | 0.4342944819032518 | Log base 10 of Euler's Constant |
Maths Functions
Maths Object have several methods to perform Maths Operations. Here is a list of JavaScript Maths Methods with example.
Math.abs()
Math.abs() method returns the positive value of given number. For positive number, it will remain same, but for negative numbers, it will convert it to positive.
Math.abs(2); // return 2
Math.abs(-2); // return 2
Math.abs(0); // return 0
Math.ceil()
Math.ceil() method will round a number up to next integer.
Math.ceil(2.3); // return 3
Math.ceil(1.9); // return 2
Math.ceil(2); // return 2
Math.floor()
Math.ceil() method will round a number down to next integer.
Math.floor(2.3); // return 2
Math.floor(1.9); // return 1
Math.floor(2); // return 2
Math.round()
Math.round() method will round a number to nearest integer. Foe example, 1.3 will become 1 and 1.6 will become 2.
Math.round(2.3); // return 2
Math.round(1.9); // return 2
Math.round(2); // return 2
Math.exp()
Math.exp() method will raise a number to the power of Eular's Constant
Math.exp(1); // return 2.718281828459045
Math.exp(0); // return 1
Math.pow()
Math.pow() method will raise any no (1st argument ) to power of (2nd argument ).
Math.pow(2,3); // return 8
Math.pow(3,4); // return 81
ES2016 (es7) introduced Exponential Operator, i.e. **. Math.pow is only recommended for Old Browsers now.
Math.sqrt()
Math.sqrt() method will return the square root of a number.
Math.sqrt(4); // return 2
Math.sqrt(81); // return 9
Math.cbrt()
Math.cbrt() method will return the cube root of a number.
Math.cbrt(27); // return 3
Math.hypot()
Math.hypot() method will return the hypotenuse of a right angled triangle by passing two arguments, base and perpendicular.
Math.Math.hypot(1,1); // return 1.4142135623730951
Math.Math.hypot(3,4); // return 5
Math.log()
Math.log() method will return the natural logarithm of a number.
Math.log(1); // return 0
Math.log(2); // return 0.6931471805599453
Math.log(Math.E); // return 1
Math.max()
Math.max() method will return the maximum number from given arguments.
Math.max(1,2); // return 2
Math.max(3,5); // return 5
const array=[2,5,1];
Math.max(...array); // return 5
Math.min()
Math.min() method will return the minimum number from given arguments.
Math.min(1,2); // return 1
Math.min(2,3); // return 2
const array=[2,5,1];
Math.min(...array); // return 1
Trigonometric Functions
Trigonometry functions are also used in javascript to work with geometrical objects. JavaScript supports all trigonometry like, sin, cos, tan etc.
Math.sin()
Math.sin() method is used return sine of angle.
The default unit is radians, i.e PI.
To get sine of deg, use (radian * Math.PI/180).
Math.sin(90); // 0.8939966636005579
Math.sin(90*Math.PI/180); // 1
Math.sin(30*Math.PI/180); // 0.49999999999999994
Math.cos()
Math.cos() method is used return cosine of angle.
Math.cos(0); // 1
Math.cos(30); // 0.15425144988758405
Math.cos(30*Math.PI/180); // 0.8660254037844387
Math.tan()
Math.tan() method is used return tangent of angle.
Math.tan(0); // 0
Math.tan(30); // -6.405331196646276
Math.tan(45); // 1.6197751905438615
Math.tan(60); // 0.320040389379563
To get cosec, sec and cot, divide sin, sec or tan by 1. exp
- cosec 30 = 1/(Math.sin(30*Math.PI/180))
- sec 45 = 1/(Math.cos(45*Math.PI/180))
- cot 60 = 1/(Math.tan(60*Math.PI/180))
Random Numbers
Math.random() method is used to get any random number between 0 and 1.
Math.random(); // any random no between 0 to 1
To get random number between 0 and any other number, multiply Math.random() with the same number.
Math.random()*10; // any random no between 0 to 10
Math.random()*100; // any random no between 0 to 100
Get random integer
To get random integer, use Math.floor(Math.random).
Math.floor(Math.random()*10);
Build a DICE
To get random integer between 1-6, use Math.floor(Math.random()*6+1)
Math.ceil(Math.random()*6); // 1-6
Math.floor(Math.random()*6+1); // 1-6