6 Example(s) of JavaScript Math Object


Description :

Math is inbuilt object in javaScript and has lots of function for mathematical operations. In this example, We are using sqrt function of Math Object. Math sqrt function: Math.sqrt(16) this will result in 4. See the code snippet:


JavaScript Math Object Example - 1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
    
</head>
<body>
<p id="sample"></p>
<script>
var result = Math.sqrt(16);
document.getElementById("sample").innerHTML = result;
</script>

</body>
</html>

Output

Description :

javaScript Math.PI will return the PI value. See the code snippet:


JavaScript Math Object Example - 2
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
    
</head>
<body>
<p id="sample"></p>
<script>
var result = Math.PI;
document.getElementById("sample").innerHTML = result;
</script>
</body>
</html>

Output

Description :

Math.E will return the Euler's number which is 2.718281828459045 approx. See the code snippet:


JavaScript Math Object Example - 3
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
    
</head>
<body>
<p id="sample"></p>
<script>
var result = Math.E;
document.getElementById("sample").innerHTML = result;
</script>
</body>
</html>

Output

Description :

javaScript Math.random() function will return the random number. See the code snippet:


JavaScript Math Object Example - 4
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
    
</head>
<body>
<p id="sample"></p>
<script>
var result = Math.random();
document.getElementById("sample").innerHTML = result;
</script>
</body>
</html>

Output

Description :

javaScript Math.ceil will return the minimum integer number greater than the value provided. Math.ceil(4.2) will return 5. See the code snippet:


JavaScript Math Object Example - 5
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
    
</head>
<body>
<p id="sample"></p>
<script>
var result = Math.ceil(4.2);
document.getElementById("sample").innerHTML = result;
</script>
</body>
</html>

Output

Description :

javaScript Math.floor() function returns minimum integer value than the value provided. Math.floor(4.6) will return 4. See the code snippet:


JavaScript Math Object Example - 6
<!DOCTYPE html>
<html>
<head>
<title>Welcome to LearnKode - A code learning platform</title>
    
</head>
<body>
<p id="sample"></p>
<script>
var result = Math.floor(4.6);
document.getElementById("sample").innerHTML = result;
</script>
</body>
</html>

Output