Math abs in Javascript
Overview
The Math.abs() function in javascript returns the absolute value of a number.
The absolute value of a number is the magnitude of that number without its sign, the absolute value can never be negative.
Syntax of Math abs in Javascript
Syntax:
Here, x is a number and this function returns the absolute value of x.
Parameters of Math abs in Javascript
The parameter of math abs in javascript is a number of which the absolute value is to be find. The absolute value of a number is the magnitude of that number without regard to its sign. Example: Absolute value of 10 is 10, Absolute value of -10 is also 10. Absolute value can never be negative.
Math.abs(x) where x a number.
The Return Value of Math abs in Javascript
The return value of math abs in javascript:
- The absolute value of the number is passed as a parameter.
- NaN if a non-numeric string is passed as a parameter.
- Returns 0, if null, an empty string or an empty array is passed.
Example
Output:
What is Math.abs() in JavaScript and How to Use it?
Math.abs() in JavaScript returns the absolute value of a number. The abs() function is a static function of Math, therefore we always use it with the class name like Math.abs().
To use Math.abs() function, we simply write Math.abs() and pass a number as parameter. Example:
More Examples
Output:
Supported Browsers
The list of browsers that support the Math abs in JavaScript is listed below:
- Chrome v1.0+
- Edge v12.0+
- Firefox v1.0+
- IE v3.0+
- Safari v1.0+
- Opera v3.0+
- Webview android v4.4+
Conclusion
- Math.abs() is a static method of Math class in javascript.
- Math.abs() take a number as parameter.
- It returns an absolute value if a parameter is a number.
- It returns, NaN if a parameter is a non-numeric string.
- It returns, 0 if a parameter is a null, an empty string, or an empty array.