Math Object in Javascript
Overview
Do you ever wonder about how to perform various mathematical operations like- calculating the square root of a given number, ceil and floor value of a given number, the natural logarithm of a given number, etc? Javascript math object provides methods and properties which can be used to perform various mathematical operations.
In this article, we will learn about math objects and how to perform various operations using them.
What is JavaScript Math Object?
A javascript math object is a built-in object that has properties and methods to perform various mathematical operations. Using these methods and properties we can perform various mathematical operations like calculating the square root of a given number, cube root, floor and ceil value, tangent, natural log, etc.
Static Properties
These are the following static properties of Javascript math object i.e. the values for some global constant terms such as pi(π). It can be used to perform various operations:
Math.E
The Math.E properties represent the Euler number which is the base of natural logarithms(e), and its approximate value is 2.718.
Let's take an example to see this-
Output-
In the example above, we logged the Math.E property, as result, it prints 2.718281828459045 on the console which is the approximate value of the Euler number. It verifies that the Math.E property represents the Euler number.
Math.LN2
The Math.LN2 property stands for the natural logarithm of 2, its approximate value is 0.693.
Let's take an example to see this-
Output-
In the example above, we logged the Math.LN2 property on the console. As result, it prints 0.6931471805599453 which is an approximate value of the natural logarithm of 2. It verifies that the Math.LN2 property represents the natural logarithm of 2.
Math.LN10
The Math.LN10 property represents the natural logarithm of 10, its approximate value is 2.303.
Let's take an example to see this-
Output-
In the example above, we logged the Math.LN10 properties on the console. As result, we print the 2.302585092994046 which is an approximate value of the natural logarithm of 10. It verifies that the Math.LN10 property represents the natural logarithm of 10.
Math.LOG2E
The Math.LOG2E property represents the base 2 logarithm of e, which is approximately 1.442.
Let's take an example to see this-
Output-
In the above example, we logged the Math.LOG2E property on the console. As result, it prints 1.4426950408889634 which is the approximate value of the base 2 logarithm of e. It verifies that the Math.LOG2E property represents the base 2 logarithm of e.
Math.LOG10E
The Math.LOG10E property represents the base 10 logarithm of e, which is approximately 0.434.
Let's take an example to see this-
Output-
In the above example, we logged the Math.LOG10E property on the console. As result, it prints 0.4342944819032518 which is an approximate value of the base 10 logarithm of e. It verifies that the Math.LOG10E property represents the base 10 logarithm of e.
Math.PI
The Math.PI property represents the value of PI(ratio of circumference divided by diameter), which is approximately 3.14159.
Let's take an example to see this-
Output-
In the example above, we logged the Math.PI property on the console. As result, it prints 3.141592653589793 which is the approximate value of PI(ratio of circumference/diameter). It verifies that Math.PI properly represents the value of PI( ).
Math.SQRT1_2
The Math.SQRT1_2 property represents the square root of 1/2 which is approximately .707.
Let's take an example to see this-
Output-
In the example above, we logged the Math.SQRT1_2 property on the console. As result, it prints 0.7071067811865476 which is the approximate value of the square root of 1/2. It verifies that the Math.SQRT1_2 property represents the value of the square root of 1/2.
Math.SQRT2
The Math.SQRT2 property represents the square root of 2 which is approximately 1.414.
Let's take an example to see this-
Output-
In the example above, we logged the Math.SQRT2 property on the console. As result, it prints 1.4142135623730951 which is the approximate value of the square root of 2. It verifies that the Math.SQRT1_2 property represents the value of the square root of 2.
Math Property
Property | Description |
---|---|
Math.E | Represent the Euler number which is the base of natural logarithms(e), and its approximate value is 2.718. |
Math.LN2 | Stands for the natural logarithm of 2, which is approximately 0.693 |
Math.LN10 | Stands for the natural logarithm of 10, which is 2.303. |
Math.LOG2E | Represent the base 2 logarithm of e, which is approximately 1.442. |
Math.LOG10E | Represent the base 10 logarithm of e, which is approximately 0.434. |
Math.PI | Represent the value of PI(ratio of circumference/diameter), which is approximate 3.14159. |
Math.SQRT1_2 | Represent the square root of 1/2 which is approximately .707. |
Math.SQRT2 | Represent the square root of 2 which is approximately 1.414. |
Math Methods
Method | Description |
---|---|
sqrt() | Return the square root of a given number. |
round() | Return the value of a given number rounded to its nearest integer. |
sin() | Return the sin of a given number. |
tan() | Return the tangent of a given number. |
abs() | Return the absolute value of a given number. |
acos() | Return the arccosine of a given number. |
asin() | Return the arcsine of a given number. |
atan() | Return the arctangent of a given number. |
atan2() | Return the arctangent of the quotient of its arguments. |
ceil() | Return the ceil value of a given number. |
floor() | Return the floor value of a given number. |
cos() | Return the cosine of a given number. |
exp() | It returns e to the power of x, where x is the exponent and e is Euler's number |
log() | Return the natural logarithm of a given number. |
max() | Return the largest value of a given set of numbers. |
min() | Return the smallest value of a given set of numbers. |
pow() | Return the value of base to exponent power (base exponent). |
random() | Return a random number from 0 to 1 (1 is inclusive) |
Example
Converting Between Degrees and Radians
In this example, we will write a program to convert a degree value into a radian value and vice-versa.
- Degree to Radian To convert the degree into radian, we have to multiply the degree with the value of PI and divide by 180. To implement this in code we will write the following code-
Output-
In the above example, we declare a degToRad function in which we are taking the degree value degVal as a parameter. Inside the degToRed function we divide the value of PI by 180 and multiply with the degree value(degVal * (Math.PI / 180)) then returned the final result.
When we called the degToRad function by passing 50 as an argument, as result, it prints 0.8726646259971648 on the console which is a converted value of a degree to radian.
- Radian to degree To convert radian into a degree, we have to multiply the radian by 180 and divide by the value of PI, it's like doing the opposite of Degree to radian. To implement this in code we will write the following code-
Output
In the example above, we made a function redToDeg in which we are taking the radian value redVal as a parameter. Inside the redToDeg function we divide 180 by the value of PI and multiply with radian(redVal * (180 / Math.PI)) then returned the final result.
When we called the redToDeg function by passing 0.8726646259971648 as an argument, it prints 50 on the console as a result, which is converted value of the radian into the degree.
Calculating the Height of an Equilateral Triangle
In this example, we will make a program that will calculate the height of an equilateral triangle.
To calculate the height of an equilateral triangle we have to multiply the side of the triangle into the square root of three divided by two(h = (a√3)/2). Here, we will use the Math.sqrt() method to calculate the square root of 3 and will write the following code to calculate the height of an equilateral triangle-
Output-
In the above example, we made a function triTri in which we are taking the side of the triangle as the side parameter. Inside the triTri function we calculated the square root of 3, divided by 2, multiplied with the side of the triangle (side) then returned the final result.
When we called the equTri function by passing 3 as an argument, it prints 2.598076211353316 on the console as a result, which is the height of an equilateral triangle.
Returning a Random Integer Between Two Bounds
In this example, we will make a program that will return a random integer within a given value range.
To make a function that will return a random integer, we will use the Math.random() and Math.floor() methods by writing the following code-
Output-
In the example above, we made a function getRandomInt which takes max and min value as parameters and return a random integer between a given range.
When we call the getRandomInt by passing 10 and 20, as result, it prints a random number between 10 and 20 on the console.
Browser Compatibility
It's fully supported in all major browsers:
Browser | Chrome | Firefox | Edge | Opera | Safari |
---|---|---|---|---|---|
Version | 1 | 1 | 12 | 3 | 1 |
Support | Yes | Yes | Yes | Yes | Yes |
Conclusion
- Math object is a built-in object in Javascript
- The math object provides various properties and methods to perform various operations.
- Math objects can't be made using a constructor.