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


Example of JavaScript absolute value

Basic Example

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


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.


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

Your email address will not be published. Required fields are marked *