The abs function in javascript is used to calculate the absolute value of a number. In this article, we will explain how to use the abs function in javascript through examples. For more information about coders, see.
Basic abs method syntax
Math. abs (number)
Parameter Introduction
Parameters |
Description |
Number |
Required. The abs method needs to calculate the numerical expression of the absolute value. |
Return Value
The returned value is the absolute value of the number parameter.
Instance
The following example demonstrates the usage of the abs method.
Function ComparePosNegVal (n) {var s; var v1 = Math. abs (n); var v2 = Math. abs (-n); if (v1 = v2) s = n + "and" s + =-n + "have the same absolute values. "; Return (s);} document. write (ComparePosNegVal (7 ));
Online operation
The running result is:
The absolute values of 7 and-7 are the same.