Hey, guys, welcome back. In this module, I will be talking about Math in JavaScript. In the previous modules, we studied Objects and Date in JavaScript. So, let’s begin with the Math object in JavaScript.
Math in JavaScript
The Math object in JavaScript allows the programmer to perform mathematical operations on numbers using methods and constants or properties. It is a built-in object in JavaScript that cannot be instantiated.
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(Math);
When we print Math, we will be getting all the constants or properties and methods of the Math object in JavaScript.
console.log(typeof(Math)); //Output //object
The Math object in JavaScript has many properties and methods. So, let’s discuss some of them.
Math properties or Math constants in JavaScript
In JavaScript, 8 mathematical constants can be directly used by just using the Math keyword along with the property name or constant.
Syntax
Math.property_name
Let’s know about them.
- Math.PI
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
- Math.E
It is an Euler’s constant and returns the Euler’s number.console.log(Math.E) //Output //2.718281828459045
- Math.LN2
It is used to return the natural logarithm of 2.console.log(Math.LN2) //Output //0.6931471805599453
- Math.LN10
It is a math property that returns a natural logarithm of 10.console.log(Math.LN10) //Output //2.302585092994046
- Math.LOG2E
It is a math property that is used to return base 2 logarithms of E.console.log(Math.LOG2E) //Output //1.4426950408889634
- Math.LOG10E
It is a math object property that returns the base 10 logarithms of E.console.log(Math.LOG10E) //Output //0.4342944819032518
- Math.SQRT2
This property or constant is used to return the square root of 2.console.log(Math.SQRT2) //Output //1.4142135623730951
- Math.SQRT1_2
This math object property returns the square root of ½.console.log(Math.SQRT1_2) //Output //0.7071067811865476
Math methods in JavaScript
The Math object has many methods to perform mathematical operations or tasks on numbers.
Syntax
Math.method(number)
- ceil()
It is a method that takes a single number as a parameter and rounds up the given number to return its larger nearest integer.
Syntax
Math.ceil(number)console.log(Math.ceil(3.5)); console.log(Math.ceil(-5.2)); //Output //4 //-5
- floor()
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.
Syntax
Math.floor(number)console.log(Math.floor(3.5)); console.log(Math.floor(-5.2)); //Output //3 //-6
- round()
This method is used to round-off the given number and return the nearest integer value of a given number.
Syntax
Math.round(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.
- trunc()
It is used to return the integer value i.e., value before the decimal point. This method was introduced in ECMAScript 2015 or ES6.
Syntax
Math.trunc(number)console.log(Math.trunc(8.4)); console.log(Math.trunc(-4.4)); console.log(Math.trunc(-7.7)); //Output //8 //-4 //-7
- max()
It is a method that takes multiple numbers as a parameter and returns a maximum value among them.
Syntax
Math.max(number1, number2, number3,…..)console.log(Math.max(4.7,5,-3,0,2,-9.3,7)); //Output //7
- min()
It is a method that takes multiple numbers as a parameter and returns a minimum value among them.
Syntax
Math.min(number1, number2, number3,…..)console.log(Math.min(4.7,5,-3,0,2,-9.3,7)); //Output //-9.3
- sqrt()
It is used to return the square root of the given number.
Syntax
Math.sqrt(value)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
- cbrt()
It is used to return the cube root of the given number.
Syntax
Math.cbrt(value)console.log(Math.cbrt(-12)); console.log(Math.cbrt(8)); //Output //-2.2894284851066637 //2
- pow()
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.
Syntax
Math.pow(base, exponent)console.log(Math.pow(4,2)); console.log(Math.pow(-3,4)); //Output //16 //81
- random()
It is a math object method used to generate random numbers between 0 (inclusive) and 1(exclusive).
Syntax
Math.random()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:
console.log(Math.floor(50+(100-50)*Math.random()));
Every time you run the code, you will be getting different values in the range 50 and 100.
- abs()
This is used to return the absolute or positive value of the given number.
Syntax
Math.abs(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
There are some methods in the Math object in JavaScript which cannot be discussed in a single article. Given below is the list of other math methods, you can try it by yourself.
- sign()
- sin()
- cos()
- tan()
- log()
- sinh()
- cosh()
- tanh()
- exp()
- hypot()
- asin()
- acos()
- atan()
I hope you have gained a good knowledge of the Math in JavaScript. Try to implement all the methods and constants or properties discussed above to gain a better understanding. Stay connected with us, for more informative modules like this. Keep practicing!