- JavaScript Absolute Value — Math.abs()
- Table of Contents — Absolute Value Python:
- TL;DR — How to find the absolute value in JavaScript?
- JavaScript Absolute Value:
- How to use the Math.abs() function?
- JavaScript Absolute Value — Syntax:
- Math.abs()
- Try it
- Syntax
- Parameters
- Return value
- Description
- Examples
- Using Math.abs()
- Coercion of parameter
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- JavaScript Absolute Value — Math.abs()
- Table of Contents — Absolute Value Python:
- TL;DR — How to find the absolute value in JavaScript?
- JavaScript Absolute Value:
- How to use the Math.abs() function?
- JavaScript Absolute Value — Syntax:
- Parameters:
- Return Values:
- Code and Explanation:
- Closing Thoughts — JavaScript Absolute method:
- JavaScript Math.abs()
- JavaScript Rounding Functions
- Syntax
- Parameters
- Return Value
- Related Pages:
- Browser Support
- COLOR PICKER
- Report Error
- Thank You For Helping Us!
- How to find absolute value in JavaScript
- How does JavaScript Math.abs() method works
- How to use the JavaScript Math.abs() method
- How to use the Math.abs() method with the positive number
- How to use the Math.abs() method with the negative number
- How to use the Math.abs() method with the null value
- Conclusion
JavaScript Absolute Value — Math.abs()
In this short tutorial, we look at the JavaScript absolute value (math.abs()) method. We explain the syntax with a real-world example.
Table of Contents — Absolute Value Python:
TL;DR — How to find the absolute value in JavaScript?
The math.abs() function is used to return the absolute value in JavaScript. It negates the native sign of a number and returns the relevant positive value.
console.log(Math.abs(-2)); //Output = 2
JavaScript Absolute Value:
JavaScript Absolute value is a method of the Math object in JavaScript. This method helps return the absolute values of a number. Absolute value or modules essentially means a non-negative value of x.
To understand the math involved let’s first understand what absolute value actually means. Absolute value is the distance between any number and 0 on the number line. Since it is the distance, there are no negative values.
Subsequently, when a 0 is passed, JavaScript returns 0 as the distance would also be 0.
How to use the Math.abs() function?
Using the Math.abs() method is quite straightforward. The only thing that you have to keep in mind is that abs() is a static method of Math. Hence you would have to add the Math. prefix in case you want to use it.
JavaScript Absolute Value — Syntax:
Math.abs()
The Math.abs() static method returns the absolute value of a number.
Try it
Syntax
Parameters
Return value
The absolute value of x . If x is negative (including -0 ), returns -x . Otherwise, returns x . The result is therefore always a positive number or 0 .
Description
Because abs() is a static method of Math , you always use it as Math.abs() , rather than as a method of a Math object you created ( Math is not a constructor).
Examples
Using Math.abs()
.abs(-Infinity); // Infinity Math.abs(-1); // 1 Math.abs(-0); // 0 Math.abs(0); // 0 Math.abs(1); // 1 Math.abs(Infinity); // Infinity
Coercion of parameter
Math.abs() coerces its parameter to a number. Non-coercible values will become NaN , making Math.abs() also return NaN .
.abs("-1"); // 1 Math.abs(-2); // 2 Math.abs(null); // 0 Math.abs(""); // 0 Math.abs([]); // 0 Math.abs([2]); // 2 Math.abs([1, 2]); // NaN Math.abs(>); // NaN Math.abs("string"); // NaN Math.abs(); // NaN
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Feb 21, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
JavaScript Absolute Value — Math.abs()
In this short tutorial, we look at the JavaScript absolute value (math.abs()) method. We explain the syntax with a real-world example.
Table of Contents — Absolute Value Python:
TL;DR — How to find the absolute value in JavaScript?
The math.abs() function is used to return the absolute value in JavaScript. It negates the native sign of a number and returns the relevant positive value.
console.log(Math.abs(-2)); //Output = 2
JavaScript Absolute Value:
JavaScript Absolute value is a method of the Math object in JavaScript. This method helps return the absolute values of a number. Absolute value or modules essentially means a non-negative value of x.
To understand the math involved let’s first understand what absolute value actually means. Absolute value is the distance between any number and 0 on the number line. Since it is the distance, there are no negative values.
Subsequently, when a 0 is passed, JavaScript returns 0 as the distance would also be 0.
How to use the Math.abs() function?
Using the Math.abs() method is quite straightforward. The only thing that you have to keep in mind is that abs() is a static method of Math. Hence you would have to add the Math. prefix in case you want to use it.
JavaScript Absolute Value — Syntax:
Parameters:
Return Values:
Code and Explanation:
The best way to familiarise yourself with the JavaScript Absolute method is to practice and try breaking it. In the below code we have used math.abs() methods on a list of values.
Math.abs(-10); // 10 Math.abs(10); // 10 Math.abs('-10'); // 10 Math.abs(''); // 0 Math.abs([]); // 0 Math.abs(null); // 0 Math.abs([2]); // 2 Math.abs([1,2]); // NaN Math.abs(<>); // NaN Math.abs('Ten'); // NaN Math.abs(); // NaN
Another important point to remember while using the math.abs() methods, is it converts strings containing a number and returns its absolute value.
Closing Thoughts — JavaScript Absolute method:
This method is mostly used before displaying a particular value. A common example would be while displaying distance on a map. In cases where you cross your destination, you are not returned with a negative value but rather the absolute value from the destination.
JavaScript Math.abs()
The Math.abs() method returns the absolute value of a number.
JavaScript Rounding Functions
Syntax
Parameters
Return Value
Type | Description |
Number | The absolute value of the number. |
Related Pages:
Browser Support
Math.abs() is an ECMAScript1 (ES1) feature.
ES1 (JavaScript 1997) is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | Yes |
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.
How to find absolute value in JavaScript
The JavaScript Maths.abs() method provides a utility to attain the absolute value of a number. The abs() method is an absolute function of maths and therefore, it is used as the Math.abs() method. Absolute value is a positive value or a value without negation.
Math.abs() method belongs to ECMAScript1 and is supported by all the browsers. This guide will describe the detailed knowledge of the JavaScript Math.abs() method with the following outcomes.
How does JavaScript Math.abs() method works
Math.abs() is a static function of JavaScript and returns the absolute value. Let’s refer to the following syntax of the Math.abs() method.
Here, ‘n’ is a parameter and Math.abs() retrieves the absolute value of ‘n’.
Math.abs() method returns the same output if the ‘n’ is positive or zero. However, if the given number is negative then it removes the negation and returns a positive number.
How to use the JavaScript Math.abs() method
Javascript Math.abs() function is commonly used to acquire the absolute value of a given number. It refers to its distance from the minimum value on the number line without any direction.
How to use the Math.abs() method with the positive number
Here, we’ll show you how Math.abs() method behaves when a positive number is passed to it.
In the above code, a positive value ‘37’ is passed to the Math.abs() method and the result of the Math.abs() method is stored in a variable.
The output showed that the function returned the same value as an output because the Math.abs() function returns the absolute values only. Therefore, the returned output number is ‘37’.
How to use the Math.abs() method with the negative number
This example demonstrates the working of the Math.abs() method if a negative number is passed to it.
In this code, the negative value ‘-18’ is passed as a parameter to the Math.abs() function.
The output showed that the function returned the positive value of the ‘18’. We know that the Math.abs() function only returns the absolute values.With the negative values, this function removes the negation sign and converts them into positive values.
How to use the Math.abs() method with the null value
The following code is practised to understand the working of Math.abs() in presence of a null value.
In the above code, a null value is passed to the Math.abs() function.
The output showed that the Math.abs() function returned the ‘0’ when a null value is passed to a parameter. It is the property of Math.abs() method that the function returns the ‘0’ for an empty string.
Conclusion
In JavaScript, the Math.abs() function is used to obtain the absolute value of a number. Math.abs() changes the sign of negative number. However, it returns the same number in case of a positive number or zero value. This post demonstrates the working and usage of the Math.abs() method in JavaScript.