Skip to content

JavaScript absolute value | abs method example code

  • by

Use the abs method to get absolute value in JavaScript. JavaScript Math.abs() method is return the absolute value of a number. It takes a number as a parameter.

Syntax of abs method

Math.abs(value)

Example of JavaScript absolute value

Basic Example

<!DOCTYPE html> 
    <html> 
    <body> 
            <script language="JavaScript">
                var num = -10.85;
                var n = Math.abs(num)
                alert(n);
            </script>     
    </body> 
    </html> 

Output:

JavaScript absolute value

More Examples

var a = Math.abs(10); //10
var b = Math.abs(-10.25); //10.25
var c = Math.abs(null); // 0
var d = Math.abs("Hello"); // NaN
var e = Math.abs(7+3);// 10

Q: How to get the absolute value of a number in JavaScript / JS?

Answer: Use the Math.abs() function, It returns the absolute value. the abs() function is a static function of the Math object, it must be invoked through the placeholder object called Math.

Math.abs(number);

Do comment if you have any doubts and suggestion on topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading