- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM Node Lists
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
- JavaScript Useful Resources
- JavaScript - Questions And Answers
- JavaScript - Quick Guide
- JavaScript - Functions
- JavaScript - Resources
JavaScript - The Math Object
The math object provides you properties and methods for mathematical constants and functions. Unlike other global objects, Math is not a constructor. All the properties and methods of Math are static and can be called by using Math as an object without creating it.
Thus, you refer to the constant pi as Math.PI and you call the sine function as Math.sin(x), where x is the method's argument.
Syntax
The syntax to call the properties and methods of Math are as follows
var pi_val = Math.PI; // Property var sine_val = Math.sin(30); // Method
Math Properties
Here is a list of all the properties of Math and their description.
Sr.No. | Property & Description |
---|---|
1 | E
Euler's constant and the base of natural logarithms, approximately 2.718. |
2 | LN2
Natural logarithm of 2, approximately 0.693. |
3 | LN10
Natural logarithm of 10, approximately 2.302. |
4 | LOG2E
Base 2 logarithm of E, approximately 1.442. |
5 | LOG10E
Base 10 logarithm of E, approximately 0.434. |
6 | PI
Ratio of the circumference of a circle to its diameter, approximately 3.14159. |
7 | SQRT1_2
Square root of 1/2; equivalently, 1 over the square root of 2, approximately 0.707. |
8 | SQRT2
Square root of 2, approximately 1.414. |
In the following sections, we will have a few examples to demonstrate the usage of Math properties.
Math Methods
Here is a list of the methods associated with Math object and their description
Sr.No. | Method & Description |
---|---|
1 | abs()
Returns the absolute value of a number. |
2 | acos()
Returns the arccosine (in radians) of a number. |
3 | acosh() Returns the inverse hyperbolic consine of a number. |
4 | asin()
Returns the arcsine (in radians) of a number. |
5 | asinh() Returns the inverse hyperbolic sine of a number. |
6 | atan()
Returns the arctangent (in radians) of a number. |
7 | atan2()
Returns the arctangent of the quotient of its arguments. |
8 | atanh() Returns the inverse hyperbolic tangent of a number. |
9 | cbrt() Finds a cube root of a given number. |
10 | ceil()
Returns the smallest integer greater than or equal to a number. |
11 | clz32() Returns the number of leading zero in 32-bit binary number. |
12 | cos()
Returns the cosine of a number. |
13 | cosh() It returns the hyperbolic cosine of a number. |
14 | exp()
Returns EN, where N is the argument, and E is Euler's constant, the base of the natural logarithm. |
15 | expm1() Returns EN - 1, where N is the argument, and E is Euler's constant, the base of the natural logarithm. |
16 | floor()
Returns the largest integer less than or equal to a number. |
17 | fround() Returns a nearest 32-bit single precision float representation of the number. |
18 | hypot() Calculates the square root of the sum of squares of arguments. |
19 | imul() Calculates the 32-bit multiplication of parameters. |
20 | log()
Returns the natural logarithm (base E) of a number. |
21 | log10() Returns the logarithm (base 10) of a number. |
22 | log1p() Return the natural logarithm (base E) of 1 + N, where N is an argument. |
23 | log2() Returns the base 2 logrithm of a number. |
24 | max()
Returns the largest of zero or more numbers. |
25 | min()
Returns the smallest of zero or more numbers. |
26 | pow()
Returns base to the exponent power that is, base exponent. |
27 | random()
Returns a pseudo-random number between 0 and 1. |
28 | round()
Returns the value of a number rounded to the nearest integer. |
29 | sign() Return -1 or 1 indicating the sign of the number. |
30 | sin()
Returns the sine of a number. |
31 | sinh() Return the hyperbolic sin. |
32 | sqrt()
Returns the square root of a number. |
33 | tan()
Returns the tangent of a number. |
34 | tanh() Returns the hyperbolic tangent of the number. |
35 | trunc()
Returns the integer part of the number. |
In the following sections, we will have a few examples to demonstrate the usage of the methods associated with Math.
Examples
Example: Math Properties
The example below demonstrates that each property of the Math object has a constant value.
Here, we have accessed the values of the 'E', 'LN2’, and 'PI', etc., properties.
<html> <head> <title> JavaScript - Math object's properties </title> </head> <body> <p id = "output"> </p> <script> document.getElementById("output").innerHTML = "Math.E == " + Math.E + "<br>" + "Math.LN2 == " + Math.LN2 + "<br>" + "Math.LN10 == " + Math.LN10 + "<br>" + "Math.PI == " + Math.PI + "<br>"+ "Math.LOG2E == " + Math.LOG2E + "<br>" + "Math.LOG10E == " + Math.LOG10E; </script> </body> </html>
Output
After executing the above program, it returns the values of the provided Math properties.
Math.E == 2.718281828459045 Math.LN2 == 0.6931471805599453 Math.LN10 == 2.302585092994046 Math.PI == 3.141592653589793 Math.LOG2E == 1.4426950408889634 Math.LOG10E == 0.4342944819032518
Example: Math ceil() method
Here, we are computing the JavaScript ceil() method to return the smallest larger integer value than the number passed as an argument. Here, the method returns 6 for the 5.9 value.
<html> <head> <title> JavaScript - Math.ceil() method </title> </head> <body> <p id = "output"> </p> <script> let ans = Math.ceil(5.9); document.getElementById("output").innerHTML = "Math.ceil(5.9) = " + ans; </script> </body> </html>
Output
After executing the above program, it returns the result as 6.
Math.ceil(5.9) = 6
Example: Math max() method
The Math.max() method is used to get the maximum value among the arguments passed as an array.
Here, we have passed six arguments to the Math.max() object, and the method returns the maximum value from them.
<html> <head> <title> JavaScript - Math.max() method </title> </head> <body> <p id = "output"> </p> <script> let ans = Math.max(100, 10, -5, 89, 201, 300); document.getElementById("output").innerHTML = "Math.max(100, 10, -5, 89, 201, 300) = " + ans + "<br>"; </script> </body> </html>
Output
After executing the above program, it returns 300 as maximum value.
Math.max(100, 10, -5, 89, 201, 300) = 300
Example: Math cos() method
The Math.cos() method returns the cosine value of the number passed as an argument. The cosine value of 0 is 1, which you can see in the output of the example below.
<html> <head> <title> JavaScript - Math.cos() method </title> </head> <body> <p id = "output"> </p> <script> let ans = Math.cos(0); document.getElementById("output").innerHTML = "Math.cos(0) = " + ans; </script> </body> </html>
Output
If we execute the above program, it returns "1" as result.
Math.cos(0) = 1