The Wayback Machine - https://web.archive.org/web/20240930162228/https://www.geeksforgeeks.org/javascript-math-object/
Open In App

JavaScript Math Object

Last Updated : 30 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript Math object is used to perform mathematical operations on numbers. All the properties of Math are static and unlike other objects, it does not have a constructor.

We use Math only on Number data type and not on BigInt

Example 1: This example uses math object properties to return their values. 

JavaScript
console.log("Math.LN10: " + Math.LN10);
console.log("Math.LOG2E: " + Math.LOG2E);
console.log("Math.Log10E: " + Math.LOG10E);
console.log("Math.SQRT2: " + Math.SQRT2);
console.log("Math.SQRT1_2: " + Math.SQRT1_2);
console.log("Math.LN2: " + Math.LN2);
console.log("Math.E: " + Math.E);
console.log("Math.PI: " + Math.PI);

Output
Math.LN10: 2.302585092994046
Math.LOG2E: 1.4426950408889634
Math.Log10E: 0.4342944819032518
Math.SQRT2: 1.4142135623730951
Math.SQRT1_2: 0.7071067811865476
Math.LN2: 0.6931471805599453
Math.E: 2.71828...

Example 2: Math object methods are used in this example. 

JavaScript
console.log("Math.abs(-4.7): " + Math.abs(-4.7)); 
console.log("Math.ceil(4.4): " + Math.ceil(4.4)); 
console.log("Math.floor(4.7): " + Math.floor(4.7)); 
console.log("Math.sin(90 * Math.PI / 180): " +
        Math.sin(90 * Math.PI / 180)); 
console.log("Math.min(0, 150, 30, 20, -8, -200): " +
        Math.min(0, 150, 30, 20, -8, -200)); 
console.log("Math.random(): " + Math.random()); 

Output
Math.abs(-4.7): 4.7
Math.ceil(4.4): 5
Math.floor(4.7): 4
Math.sin(90 * Math.PI / 180): 1
Math.min(0, 150, 30, 20, -8, -200): -200
Math.random(): 0.7416861489868538

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

We have a complete list of JavaScript Math Object methods, to check those please go through the JavaScript Math Complete Reference article

JavaScript Math Object – FAQs

What is the Math object in JavaScript?

The Math object is a built-in object that provides properties and methods for mathematical constants and functions. It is not a constructor, so all its properties and methods are static and can be called without creating a Math object instance.

How do you use the Math object?

You use the Math object by calling its properties and methods directly. For example, Math.PI for the value of π or Math.sqrt() for calculating the square root.

How do you generate random numbers using the Math object?

You can generate random numbers using Math.random(), which returns a floating-point number between 0 (inclusive) and 1 (exclusive). To generate a random number within a specific range, you can scale and shift the result.

How do you find the maximum or minimum of a set of numbers?

You can find the maximum or minimum of a set of numbers using Math.max() and Math.min() respectively. Both methods accept zero or more arguments.


Previous Article
Next Article

Similar Reads

What is the use of Math object in JavaScript ?
The Math object in JavaScript provides a set of methods and properties for mathematical constants and functions. It is a built-in object that allows for performing mathematical operations and accessing mathematical constants without creating an instance. What is the Math Object?The Math object is an inbuilt object that has attributes and methods fo
4 min read
How to check a JavaScript Object is a DOM Object ?
Prerequisite: DOM (Document object model), Instanceof Operator DOM (Document Object Model): Document Object Model is a hierarchical representation of HTML and XML documents in a format that is easier to interpret in terms of programming. It makes manipulation of tags, elements, attributes, and classes by interpreting its structure in the form of a
2 min read
How to access an object having spaces in the object's key using JavaScript ?
Given an object and the task is to access the object in which the key contains spaces. There are a few methods to solve this problem which are discussed below: Approach 1: Create an object having space-separated keys.Use square bracket notation instead of dot notation to access the property. Example: This example implements the above approach. C/C+
2 min read
How to check if the provided value is an object created by the Object constructor in JavaScript ?
In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values. There are several methods that can be used to check if the provided value is an object created by the Object constructor in JavaScript. Using the ins
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approach is to Extract keys from Object and LowerCase to
2 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How to Search Character in List of Array Object Inside an Array Object in JavaScript ?
Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several ways to search for characters in a list of array obje
4 min read
Difference between Object.keys() and Object.entries() methods in JavaScript
Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties: Object.keys() returns an array of a given object's own enumerable property names, while Object.entries() returns an array of a given object's own enumerable string-keyed property
2 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
6 min read
How to Append an Object as a Key Value in an Existing Object in JavaScript ?
In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows. Table of Content Using JavaScript Spread (...) Ope
3 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ?
Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsing reduce()Using a Map for Multiple ValuesUsing fo
4 min read
Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
In JavaScript, Object.keys() and Object.getOwnPropertyNames() both retrieve properties of an object but differ in scope. Object.keys() returns an array of an object's own enumerable property names. In contrast, Object.getOwnPropertyNames() returns an array of all own property names, including non-enumerable properties. These methods are useful for
3 min read
How to Push an Object into Another Object in JavaScript ?
In JavaScript, the concept of pushing an object into another object involves embedding one object within another as a property. This technique is useful for organizing data structures where related information is grouped, enhancing the ability to manage and manipulate complex data sets effectively. There are several ways to push an object into anot
4 min read
How will you access the reference to same object within the object in PHP ?
In this article, we will see how we can access the reference to the same object within that object in PHP. To do that we have to use the "$this" keyword provided by PHP. PHP $this Keyword: It is used to refer to the current objectIt can only be used inside the internal methods of the class.$this keyword can refer to another object only if it is ins
2 min read
HTML | DOM Object Object
The Object object represents only HTML <object> element. We can access any <object> element by using the getElementById(); and also can create object element by using createElement(); method.Syntax: It is used to access Object element document.getElementById("id"); It is used to create object element document.createElement("object"); Pr
2 min read
How to set an object key inside a state object in React Hooks?
We can update a React hooks state object that has a nested object containing objects with index keys with the following approach, Before doing so, consider the following example: Example: Following is the default state object: const [data, setData] = useState({ name:'', contact:'', address:{ 0:{}, 1:{}, } }) Following is the desired output after up
2 min read
How to use array that include and check an object against a property of an object ?
Array.includes() Method: In JavaScript, includes() method is used to determine that a particular element is present in an array or not. It returns true if the element is present and false when it is absent. Syntax: array_name.includes(searchElement, ?fromIndex) Parameters: searchElement: The element to be search in the array.fromIndex: The index fr
3 min read
What is a potential pitfall using typeof bar === "object" to determine if bar is an object ?
The typeof operator in JavaScript returns a string that indicates the data type of the operand, whether it be a variable, function, or object. Syntax: Following is the syntax of typeof operator: typeof operand // OR typeof(operand)Parameter: It takes the following parameter: operand: The expression whose type needs to be evaluated.Example: Basic ex
4 min read
Difference between Object.values and Object.entries Methods
The object is the parent class from which all the javascript objects are inherited and these two methods are the static methods of the Object class as they are called by the class name of the Object class. JavaScript Object.values() Method In the same order as a for...in the loop, the Object.values() method returns an array of the enumerable proper
2 min read
Object is undefined" error when trying to rotate object in three.js
In this article, we will see the "Uncaught TypeError: Cannot read properties of undefined (reading 'rotation') at animate". It is a type error for a variable, in Three .js, this error occurs when the variable is declared, but the value is not assigned or the value is undefined. We can observe from the error, message that this error occurred, when t
3 min read
How to select first object in object in AngularJS?
The main problem that we are dealing with is that for an object of objects reading the object of a particular index position is not as simple as a list. We cannot loop over it using ngFor as an object is not considered an iterable. The importance of this issue may arise when the data received from any source is an object containing objects(like JSO
3 min read
How to Replace an Object in an Array with Another Object Based on Property ?
In JavaScript, replacing an object in an array with another object based on a specific property involves identifying and updating elements within the array. This task is essential when modifications to individual objects are required, such as updating values or swapping objects based on a particular property criteria. Table of Content Using map()Us
3 min read
How to Remove Multiple Object from Nested Object Array ?
Removing multiple objects from a nested object array in JavaScript involves traversing the array and filtering out the objects that meet certain criteria, such as having specific properties or values. This can be achieved using various array manipulation methods available in JavaScript. Below are the approaches used to remove the multiple object fr
3 min read
How to Replace an Object in an Array with Another Object Based on Property ?
In JavaScript, an array is a data structure that can hold a collection of values, which can be of any data type, including numbers, strings, and objects. When an array contains objects, it is called an array of objects. Table of Content Using findIndex and splice methodsUsing filter and concat methodsUsing the map methodUsing findIndex and splice m
3 min read
How to Initialize a TypeScript Object with a JSON-Object ?
To initialize a TypeScript Object with a JSON-Object, we have multiple approaches. In this article, we are going to learn how to initialize a TypeScript Object with a JSON-Object. Below are the approaches used to initialize a TypeScript Object with a JSON-Object: Table of Content Object.assign Type AssertionSpread OperatorClass InitializationUsing
3 min read
JavaScript Math.round( ) function
JavaScript Math.round( ) function is used to round the number passed as a parameter to its nearest integer. Syntax: Math.round(value) Parameters: value: The number to be rounded to its nearest integer. Example 1: Rounding Off a number to its nearest integer To round off a number to its nearest integer, the math.round() function should be implemente
2 min read
JavaScript Math tan() Method
The Javas Math.tan() method in Javascript is used to return the tangent of a number. The Math. tan() method returns a numeric value that represents the tangent of the angle. The tan() is a static method of Math, therefore, it is always used as Math.tan(), rather than as a method of a Math object created. Syntax: Math.tan(value)Parameters: This meth
2 min read
JavaScript Math cbrt() Function
The Javascript Math.cbrt() function is used to find the cube root of a number. The cube root of a number is denoted as Math.cbrt(x)=y such that y^3=x. Syntax: Math.cbrt(number) Parameters: This function accepts a single parameter. number: The number whose cube root you want to know. Return Value: It returns the cube root of the given number. The be
1 min read
JavaScript Math acosh() Method
The Javascript Math.acosh() function in Javascript is used to return the hyperbolic arc-cosine of a number. Math.acosh (x) = arcosh(x) = y ≧ 0 such that cosh (y)=x acosh() is a static method of Math, therefore it is always used as Math.acosh(), rather than as a method of a Math object created. Syntax: Math.acosh(value) Parameters: This function acc
2 min read
JavaScript Math.clz32() Function
The Math.clz32( ) function in JavaScript returns the number of leading zero bits in the 32-bit binary representation of a number. Clz32 stands for Count Leading Zeros 32. If the passed parameter is not a number, then it will be converted into a number first and then converted to a 32-bit unsigned integer. If the converted 32-bit unsigned integer is
2 min read