JavaScript provides an Inbuilt Math object to handle mathematic tasks with numbers, and in this tutorial, we will learn how to use some of the Math object properties and methods to perform those tasks.
The Math object
The Math is a static object which means we do not need to initialize it with the
new
constructor. Instead, we can directly use Math all properties and method using the Math name, without creating its object.
Math Properties Constant values
The Math object supports many properties that are used to represent constant values. There are 8 number constant values provided as the Math properties.
Syntax
Example
Output
Math Methods
Now let's see some of the Math important methods that we often use during JavaScript programming.
Number to Integer
There are 4 common Math methods that can be used to round up a floating-point or decimal number to an integer number.
- Math.round(number)
- Math.ceil(number)
- Math.floor(number)
- Math.trunc(number)
Math.round() method
The
round(num)
method accepts a number
num
as a parameter and return the nearest integer value of the number
num
.
Example
Math.ceil() method
The
ceil(num)
method also accepts a number
num
as a parameter and
round up
the number to its nearest large Integer number.
Example
Math.floor() method
The
Math.floor(num)
method accepts a number
num
as a parameter and return a
round down
integer Number of
num
.
Example
Math.trunc() method
The
trunc(num.ddd)
method remove the decimal part and only return the integer part of
num.ddd
. We can use this method to convert any decimal number to its integer value without rounding the number.
Example
Math.sign() method
The
sign(num)
method is used to check the positive, negative or null sign on the number
num
. If the value of
num
is negative the
sign()
method will return -1 if the value of
num
is positive the
sign()
will return 1 for
num
0 value it returns 0.
Example
Math.pow() method
The
Math.pow(a,b)
is used to find the power of
a
raise to the
b
.
Example
Math.sqrt() method
The
sqrt(num)
method will return the square root of number
num
.
Example
Math.abs() method
The
abs(num)
method returns the absolute positive value of the specified parameter
num
.
Example
Math.sin() method
The
sin(angle_radian)
method return the sin value (-1 to 1) of the specified radian angle. By default, the sin() method accepts value in radian but we can also convert the radian value to the degree with the following formula. Angle in radians = Angle in degrees x PI / 180.
Example
Math.cos() method
The
cos(angle_radian)
method returns the cosine value (-1 to 1) of the specified radian angle. By default, the cos() method accepts value in radian but we can also convert the radian value to the degree with the following formula. Angle in radians = Angle in degrees x PI / 180.
Example
Math.min() method
The
min(a,b,c, d........n)
method accepts a list of number arguments and return the minimum number value.
Example
Math.max() method
The
max(a,b,c, d........n)
method accepts a list of number arguments and returns the maximum number value.
Example
Math.random() method
The random() method returns a random number between o and 1, where 0 is inclusive and 1 is exclusive.
Example
If you want to generate a random number between 0 and a specific number then you can multiply the random() method with the specified number, but the specified number will be exclusive.
Example
Math.log() method
The
log(num)
method will return the natural logarithmic value of number
num
. The base for
log()
method is
Math.E
.
Example
Math.log2() method
The
log2(num)
method will return the base 2 logarithmic value of number
num
. The base for
log2()
method is 2.
Example
Math.log10() method
The
log10(num)
method will return the base 10 logarithmic value of number
num
. The base for
log10()
method is 10.
Example
Summary
- Math is a JavaScript inbuilt object.
- round() method round the number to its nearest integer value.
- The ceil() method round up the number to its nearest integer value.
- The floor() method round down the number to its nearest integer value.
- The trunc() method removes the decimal point and only returns the integer value.
- The pow(x,y) method returns the power of x to the y.
- The sqrt() method returns the square root of the number.
- The abs() method returns the positive value of the number.
- The sin() and cos() method returns the sin and cosine value of the specified radian.