How to use abs in JavaScript

In this article, I will explain use of abs() method in JavaScript.
  • 2124

JavaScript abs() Method

  • abs() method of math object returns the absolute value of a number.

Syntax

Math.abs(x)

  • x is a Required number

Example

In the following example show to abs() method returns the absolute value of various numbers.

<!DOCTYPE html>

<html>

<body>

<h3 style="background-color:Red">abs() Method Example</h3>

<p id="demo">Click the button to the absolute value of different numbers</p>

<button onclick="myFunction()">Click On</button>

<script type="text/javascript">

    function myFunction() {

        var a = Math.abs(9.25);

        document.write("First absolute Value : " + a);

        var b = Math.abs(-9.25);

        document.write("<br />second absolute Value : " + b);

        var c = Math.abs(null);

        document.write("<br />Third absolute Value : " + c);

        var d = Math.abs("Welcome");

        document.write("<br />Fourth absolute Value : " + d);

        var e = Math.abs(6 + 6);

        document.write("<br />Five absolute Value : " + e);

        var x = document.getElementById("demo");      

    }

</script>

</body>

</html>

Output1

abs 1.jpg

Output2

abs 2.jpg

You may also want to read these related articles here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.