# JavaScript – Math Object

Math object: Providing number of pre-defined methods to perform simple mathematical operations on data.

Math.sqrt() method:

``````<html>
<body>
<script>
var x = 144;
var r = Math.sqrt(x);
document.write("Square root of " + x + " is : " + r);
</script>
</body>
</html>
``````

Math.random() method: Generate random numbers

``````<html>
<body>
<script>
var x = Math.random();
var y = Math.random();
document.write("x val : " + x + "<br/>");
document.write("y val : " + y);
</script>
</body>
</html>
``````

Math.pow(m, n): find m^n value

``````<html>
<body>
<script>
var x = Math.pow(2, 3);
var y = Math.pow(5, 2);
document.write("2 power 3 is : " + x + "<br/>");
document.write("5 power 2 is : " + y);
</script>
</body>
</html>
``````

Math.floor() :

• returns the lowest integer for the given number.
• For example,
• 3 for 3.7
• 5  for 5.9

Math.ceil():

• return the largest integer for the given number.
• For example,
• 4 for 3.2
• 6 for 5.1
• 8 for 8.9

Math.round():

• returns the rounded integer near to the given number.
• If the fractional part is equal or greater than 0.5, it goes upper value as 1
• If the fractional part is less than 0.5, it goes to lower value as 0
``````<html>
<body>
<script>
document.write("floor of 3.2  is : " + Math.floor(3.2) + "<br/>");
document.write("floor of 3.8  is : " + Math.floor(3.8) + "<br/>");

document.write("ceil of 3.2  is : " + Math.ceil(3.2) + "<br/>");
document.write("ceil of 3.8  is : " + Math.ceil(3.8) + "<br/>");

document.write("round of 3.2  is : " + Math.round(3.2) + "<br/>");
document.write("round of 3.8  is : " + Math.round(3.8) + "<br/>");
</script>
</body>
</html>
``````

Math.trunc(): it returns only Integer part of specified decimal value

Math.trunc(4.9);   // returns 4

Math.sign():

• returns -1 if input value is negative
• returns 0 if input value is zero
• returns +1 if input value is positive

Example: Math.sign(-4) : returns -1

Math.cbrt() : returns cube root of x

Scroll to Top