Complete guides to Javascript Math Objects. Javascript has a builtin object that can be used to performing mathematical calculations. You don’t need to create some formula and functions manually to do the calculations that already have standardization.
For example, to get an absolute/positive value on a negative number, You can use Math.abs() function to return the positive number. In this Javascript tutorial, I will provide a complete reference to the most frequently used Math objects in Javascript and how to use it in the application.
Table of Contents
- 1 What is the Javascript Math Object?
- 2 List of Javascript Objects Math functions and constants
- 2.1 Calculation of logarithms, powers of numbers and Javascript exponentials
- 2.2 Trigonometric calculations
- 2.3 Javascript Rounding Function
- 2.4 Javascript Root Function
- 2.5 JS function to return the absolute number
- 2.6 Javascript object math function to find the highest and lowest numbers
- 2.7 Javascript random function
- 3 Conclusion
What is the Javascript Math Object?
The Math object is a Javascript object that contains mathematical functions and some constants to do accurate calculations like sin, cos, tan, exponent, square root.
The Math JavaScript object is different from the JavaScript operator. Object math aims to shorten formulas and calculations rather than using Javascript operators. (I have discussed Javascript operators, please read the Javascript operator tutorial)
Here is a list of functions of the Javascript Math object that often used
List of Javascript Objects Math functions and constants
Calculation of logarithms, powers of numbers and Javascript exponentials
Math.log(x)
Used for calculating logarithmic operations.
Example:
1 | Math.log(4) |
Math.pow()
The function used to calculate the power of numbers
Example
1 | Math.pow(2,3) |
Math.exp()
Used to calculate exponential formulas.
Example:
1 | Math.exp(1) |
Trigonometric calculations
Math.acos(x)
Returns the cosine arc between the value 0 and pi in radians
Example:
1 | Math.acos(0.7) |
Math.asin(x)
Returns the arcsine of x (x in radians)
Example:
1 | Math.asin(0.3) |
Math.atan(x)
Used to return the arctangent of x as a numerical value between pi/2 and pi/2 radians
Example:
1 | Math.atan(0.5) |
Math.atan2(y,x)
Javascript Math object to return the arctangent of the result for an argument
Example:
1 | Math.atan2(2,4) |
Math.cos(x)
Javascript object to return cosine of x (x in radians)
Example:
1 | Math.cos(0.5) |
Math.sin()
Used to return a sine of x (x is radians)
Example:
1 | Math.sin(10) |
Math.tan(x)
Used to return the tangent value from an angles in radians
Example:
1 | Math.tan(0.5) |
Javascript Rounding Function
Math.round(x)
Used to round numbers to the nearest integer number:
Example:
1 | Math.round(2.3) |
Math.floor(x)
Used for round down integer values
Example:
1 | Math.floor(1.8) |
Math.ceil(x)
Used for round up integer values
Example:
1 | Math.ceil(4.1) |
Javascript Root Function
Math.sqrt(x)
Javascript function to return square root of x
Example:
1 | Math.sqrt(64) |
Math.cbrt(x)
The Javascript function to return a cubic root of x
Example:
1 | Math.cbrt(27) |
JS function to return the absolute number
Math.abs(x)
Used to return the absolute number of x
Example:
1 | Math.abs(-44) |
Javascript object math function to find the highest and lowest numbers
Math.max(x,y,z,…,n)
Used to return a number with the highest value
js examples:
1 | Math.max(2,5,7,8) |
Math.min(x,y,z,…,n)
Used to return a number with the lowest value
Example:
1 | Math.min(2,5,7,8) |
Javascript random function
Math.random()
used to return random numbers between 0.0 to 1
Example:
1 | Math.random() |
In addition to the Math object function, Javascript also has constant math/ math property objects like pi, LN10, E
Math.E | returns Eulers Constant |
Math.PI | returns Javascript math object 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 logarithms of E |
Math.LOG10E | returns base 10 logarithms of E |
Conclusion
- Math objects are objects that are specifically used for mathematical calculations or operations.
- You will use it a lot in making programs that require mathematical calculations.
So the tutorial for learning JavaScript properties and methods in Math Objects, hopefully easy to understand.
To be good at Javascript in a short period, please read the complete Javascript tutorial articles
Leave a Reply