JavaScript Math Object example

JavascriptWeb DevelopmentObject Oriented Programming

The JavaScript Math object allows us to perform mathematical operation on numbers. It has properties and methods for mathematical constants and functions.

Following is the code to implement the JavaScript Math Object with some of its properties and methods −

Example

 Live Demo

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
   body {
      font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
   }
   .sample {
      font-size: 18px;
      font-weight: 500;
      color: red;
   }
</style>
</head>
<body>
<h1>JavaScript Math Object</h1>
<div class="sample"></div>
<div style="font-weight: bold; color: black;" class="result"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to see some math Object methods
</h3>
<script>
   let sampleEle = document.querySelector(".sample");
   document.querySelector(".Btn").addEventListener("click", () => {
      sampleEle.innerHTML = 'Math.round(7.7) = ' + Math.round(7.7) + '<br>';
      sampleEle.innerHTML += 'Math.sqrt(121) = ' + Math.sqrt(121) + '<br>';
      sampleEle.innerHTML += 'Math.pow(4,4) = ' + Math.pow(4,5) + '<br>';
   });
</script>
</body>
</html>

Output

On clicking the “CLICK HERE” button −

raja
Published on 11-May-2020 13:33:07
Advertisements