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.



  • x is a Required number


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

<!DOCTYPE html>



<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");      






abs 1.jpg


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


More Articles

© 2020 DotNetHeaven. All rights reserved.