The methods and properties of the Math object are static so it can be directly accessed just by using the Math keyword. It does not have constructors, unlike the date object.
console.log(typeof(Math)); //Output //object
Let’s know about them.
It is a constant or property that returns the value of pi used in finding the area and circumference of a circle.
console.log(Math.PI) //Output //3.141592653589793
It is an Euler’s constant and returns the Euler’s number.
console.log(Math.E) //Output //2.718281828459045
It is used to return the natural logarithm of 2.
console.log(Math.LN2) //Output //0.6931471805599453
It is a math property that returns a natural logarithm of 10.
console.log(Math.LN10) //Output //2.302585092994046
It is a math property that is used to return base 2 logarithms of E.
console.log(Math.LOG2E) //Output //1.4426950408889634
It is a math object property that returns the base 10 logarithms of E.
console.log(Math.LOG10E) //Output //0.4342944819032518
This property or constant is used to return the square root of 2.
console.log(Math.SQRT2) //Output //1.4142135623730951
This math object property returns the square root of ½.
console.log(Math.SQRT1_2) //Output //0.7071067811865476
The Math object has many methods to perform mathematical operations or tasks on numbers.
It is a method that takes a single number as a parameter and rounds up the given number to return its larger nearest integer.
console.log(Math.ceil(3.5)); console.log(Math.ceil(-5.2)); //Output //4 //-5
It is a math object method that takes a single number as a parameter and rounds off the given number in the downward direction and returns its lower nearest integer.
console.log(Math.floor(3.5)); console.log(Math.floor(-5.2)); //Output //3 //-6
This method is used to round-off the given number and return the nearest integer value of a given number.
console.log(Math.round(6.5)); console.log(Math.round(6.3)); console.log(Math.round(-3.6)); console.log(Math.round(-3.2)); //Output //7 //6 //-4 //-3
The point is to be noted that if the number after the decimal point is greater than or equal to 5, the next integer value is returned, otherwise, the number before the decimal is printed.
It is used to return the integer value i.e., value before the decimal point. This method was introduced in ECMAScript 2015 or ES6.
console.log(Math.trunc(8.4)); console.log(Math.trunc(-4.4)); console.log(Math.trunc(-7.7)); //Output //8 //-4 //-7
It is a method that takes multiple numbers as a parameter and returns a maximum value among them.
Math.max(number1, number2, number3,…..)
console.log(Math.max(4.7,5,-3,0,2,-9.3,7)); //Output //7
It is a method that takes multiple numbers as a parameter and returns a minimum value among them.
Math.min(number1, number2, number3,…..)
console.log(Math.min(4.7,5,-3,0,2,-9.3,7)); //Output //-9.3
It is used to return the square root of the given number.
console.log(Math.sqrt(5)); console.log(Math.sqrt(7.4)); //Output //2.23606797749979 //2.7202941017470885
If we give a negative number as a parameter, we will get NaN as an output.
console.log(Math.sqrt(-25)); //Output //NaN
It is used to return the cube root of the given number.
console.log(Math.cbrt(-12)); console.log(Math.cbrt(8)); //Output //-2.2894284851066637 //2
This static method takes two parameters- base and exponent as a parameter and returns a single value as base number raised to the power of exponent, i.e., base^exponent.
console.log(Math.pow(4,2)); console.log(Math.pow(-3,4)); //Output //16 //81
It is a math object method used to generate random numbers between 0 (inclusive) and 1(exclusive).
console.log(Math.random()); //Output //0.14660515039933908
If you want to generate random numbers between 50 and 100, you can do so by using the following method as shown below:
Every time you run the code, you will be getting different values in the range 50 and 100.
This is used to return the absolute or positive value of the given number.
console.log(Math.abs(-5.55)); console.log(Math.abs(6)); //Output //5.55 //6
If a null value is given as a parameter, the output will be 0.
console.log(Math.abs(null)); //Output //0
For string value given as a parameter, NaN (Not a Number) is returned as an output.
console.log(Math.abs("hii")); //Output //NaN