What is work of Math object in JS?

Asked 19-Jul-2021
Viewed 300 times

1 Answer


1

Math object, have constants and functions. It is not a function object. Math object works with number types. The Math object gives you function and properties for mathematical constants and functions. Math does not have a constructor. All properties and methods of math are static and can be called using math as an object without creating it.

syntax:
variable-name = Math.property-name or function-name();
Math.PI
Ratio of a circle's circumference to its diameter; approximately 3.14159.

Math.SQRT2
Square root of 2; approximately 1.414.

Static methods
Math.abs(x) Returns the absolute value of x.

Example

<!DOCTYPE html>

<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title></title>
</head>
<body>
<h2>JavaScript Math Object</h2>
<p>Math.PI returns PI value</p>
<p id='piDemo'></p>
<p>Math.sqrt(16) returns the square root of 16:</p>
<p id='sqrtDemo'></p>
<script>
    var x = Math.PI;
    var y = Math.sqrt(16);
    document.getElementById('piDemo').innerHTML = x;
    document.getElementById('sqrtDemo').innerHTML = y;
</script>
</body>
</html>
Output

JavaScript Math Object

Math.PI returns PI value
3.141592653589793
Math.sqrt(16) returns the square root of 16:
4