Math in JavaScript-tutorial
JavaScript provides powerful built-in mathematical functions and properties through the Math object.
These functions let you do calculations like square roots, powers, rounding, and more without writing complex logic.
Commonly Used Math Methods in JavaScript
Math.sqrt(x)
Returns the square root of number x.
Syntax:
Example:
Output
.webp)
Math.pow(x, y)
Returns x raised to the power of y.
Syntax:
Example:
Output
.webp)
Math.floor(x)
Rounds a number down to the nearest integer.
Syntax:
Example:
Output
.webp)
These functions let you do calculations like square roots, powers, rounding, and more without writing complex logic.
Commonly Used Math Methods in JavaScript
Math.sqrt(x)
Returns the square root of number x.
Syntax:
const squareRoot = Math.sqrt(16); // Returns 4
Example:
<!DOCTYPE html><html>
<body> Square Root of 17 is: <span id="p1"></span> <script> document.getElementById('p1').innerHTML = Math.sqrt(17); </script></body>
</html>
Output
.webp)
Math.pow(x, y)
Returns x raised to the power of y.
Syntax:
const result = Math.pow(2, 3); // Returns 8
Example:
<!DOCTYPE html><html>
<body> 3 to the power of 4 is: <span id="p3"></span> <script> document.getElementById('p3').innerHTML = Math.pow(3, 4); </script></body>
</html>
Output
.webp)
Math.floor(x)
Rounds a number down to the nearest integer.
Syntax:
const floored = Math.floor(3.9); // Returns 3
Example:
<!DOCTYPE html><html>
<body> Floor of 4.6 is: <span id="p4"></span> <script> document.getElementById('p4').innerHTML = Math.floor(4.6); </script></body>
</html>
Output
.webp)
Key Point
- JavaScript Math is an object with methods, not a function itself.
- Helps in rounding, power calculation, square roots, trigonometry, and more.
- No need to import – it’s built into JavaScript.