Math.abs()

JavaScript 1.0+, ECMAScript 1.0+, JScript 1.0+ Nav2+, NES2+, IE 3+, Opera3+ Syntax

							math.abs(num)

Description

The abs() method of the Math object is used to calculate the absolute value of num compared to the Math object on which it is invoked.

Example

Listing 6.151 shows how to use the abs() method and what it returns.

Listing 6.151 Example of abs() Method
 <html> <body> <script language="JavaScript"> <!–– Hide // function calculates the absolute value of the input number function doMath(){ var inputNum=document.form1.input.value; var result = Math.abs(inputNum); document.form1.answer.value = result; } // End Hide ––> </script> <form name=form1> This example calculates the absolute value of the number entered. <br><br> Enter ...

Get Pure JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.