What is work of Math object in JS?

0 votes
20 views
Ravi Vishwakarma asked 13 days ago in Web Application by Ravi Vishwakarma

What is work of Math object in JS?

1 Answer

0 votes
Ethan Karla answered 11 days ago by Ethan Karla
Math is a built-in object that has properties and methods for mathematical constants and functions. It’s not a function object. Math works with the Number type. It doesn't work with BigInt. 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.
Syntax:
 Variable-name = Math.property-name or function-name();
Example1:
<!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:</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>
Exampl2:
console.log(Math.max(1, 3, 2));

// expected output: 3
console.log(Math.max(-1, -3, -2));
// expected output: -1
const array1 = [1, 3, 2];
console.log(Math.max(...array1));
// expected output: 3