- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Math Object in JavaScript Program?
Math is an inbuilt object provided by JavaScript that has multiple attributes and methods for different mathematical functions and constants. This is not a function object.
Maths objects generally work with the Number type.
The Math object does not have a constructor and all the properties and methods are fixed or static.
The cosine function is also known as Math.cos(y). Similarly, the pi constant is known as Math.PI.
All the properties or methods provided by the Math object are static and can be called by using the Math as an object without actually creating it. In this article, we will explore different available methods and properties that are used in JavaScript.
Some static Math properties are listed below −
Property | Description | Return value |
---|---|---|
Math.E | This represents the Euler’s constant. Its default value is 2.718 approximately. | Euler’s number |
Math.LN2 | This represents the natural log which has an approx value of 0.693147180. | Natural Log of 2 |
Math.LN10 | This represents the natural log with Base 10 which has an approx value of 2.302585. | Natural Log of 10 |
Math.LOG2E | This represent Base-2 log of E with approx value of 1.442695. | Base 2 Log of E |
Math.LOG10E | This represent Base-10 log of E with approx value of 0.43429844 | Base 10 Log of E |
Math.PI | This represents the ratio of the circumference of a circle to its diameter i.e. 3.14159. | PI value |
Math.SQRT1_2 | This represents the square root of 1/2 with approx value of 0.70710678. | The square root of 1/2 |
Math.SQRT2 | This represents square root of 2 which is 1.41421356 approximately | The square root of 2 |
Example 1
In the example below, we apply different static Math properties.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <body> <script> console.log("Math.PI", Math.PI); console.log("Math.SQRT2", Math.SQRT2); console.log("Math.SQRT1_2", Math.SQRT1_2); console.log("Math.LN10", Math.LN10); console.log("Math.LN2", Math.LN2); console.log("Math.LOG10E", Math.LOG10E); console.log("Math.LOG2E", Math.LOG2E); </script> </body> </html>
Output
It will produce the following output in the Console. Math.PI
Math.PI 3.141592653589793 Math.SQRT2 1.4142135623730951 Math.SQRT1_2 0.7071067811865476 Math.LN10 2.302585092994046 Math.LN2 0.6931471805599453 Math.LOG10E 0.4342944819032518 Math.LOG2E 1.4426950408889634 Math.LOG2E 1.4426950408889634
Static Math Methods: Math objects have static methods associated with them. Some of the methods are described below.
Sr.No | Methods & Description |
---|---|
1 | Math.abs(y) This returns the positive value of y |
2 | Math.acos(y) This returns the acos value of y |
3 | Math.acosh(y) Returns hyperbolic a-cosine value of y |
4 | Math.asin(y) Returns arcsine value of y |
5 | Math.asinh(y) Returns hyperbolic arcsine value of y |
6 | Math.atan(y) Returns arctangent value of y |
7 | Math.atanh(y) Returns hyperbolic arctangent value of y |
8 | Math.atan2(y, x) Returns arctangent of the quotient of its arguments |
9 | Math.cbrt(y) Returns the cube root of y. |
10 | Math.ceil(y) Returns the smallest integer greater than or equal to y. |
11 | Math.clz32(y) Returns the number of leading zero bits of the 32-bit integer y. |
12 | Math.cos(y) Returns the cosine of the angle y. |
Example 2
In the below example we apply different Math methods such as ceil, floor, round, sin, cos, min, max, etc.
# index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> </head> <body> <h1 style="color: red;"> Welcome To Tutorials Point </h1> <script> console.log("ceil ", Math.ceil(9.6)); console.log("floor ", Math.floor(9.6)); console.log("round ", Math.round(9.6)); console.log("sine ", Math.sin(30)); console.log("cosine ", Math.cos(30)); console.log("min ", Math.min(30, 40)); console.log("max ", Math.max(30, 40)); console.log("sign ", Math.sign(-40)); console.log("abs ", Math.sign(-40)); </script> </body> </html>
Output
It will produce the following output in the Console.
ceil 10 floor 9 round 10 sine -0.9880316240928618 cosine 0.15425144988758405 min 30 max 40 sign -1 abs -1
- Related Articles
- What is math object in JavaScript?
- JavaScript Math Object example
- What are JavaScript Math Functions?
- Math. fround() function in JavaScript
- Math. hypot() function in JavaScript
- What is date object in JavaScript?
- What is arguments object in JavaScript?
- What is WeakSet Object in JavaScript?
- What is JavaScript object Destructuring?
- What is a Global Object in JavaScript?
- Implementing Math function and return m^n in JavaScript
- What is an Object Oriented Programming in JavaScript?
- Get minimum number without a Math function JavaScript
- What is the role of Navigator object in JavaScript?
- What is the importance of Navigator Object in JavaScript?
