
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the use of Math.abs() method in JavaScript?
Math.abs()
Math.abs() gives absolute value of a number, that is
p if p > 0 Math.abs(p) = |p| = { 0 if p = 0 -p if p < 0
If the above mathematical notation is observed , when p is positive the absolute value took positive p and when the value is negative absolute value take -(p).For suppose let the value of p be -5 then according to the above notation the absolute value takes -p that is -(-5) which is equal to +5.So, from this we can get that absolute value gives only positive value irrespective of sign of the number whether positive or negative.
In the following example the absolute value of the difference between 1 and 9 is positive despite the actual value is negative(-8).
Example-1
<html> <body> <script> function myDiff(a, b) { return Math.abs(a - b); } document.write(myDiff(1, 9)); </script> </body> </html>
Output
8
Example-2
The following example returns a positive value rather than given negative value using Math.abs() method.
<html> <body> <p id="absolute"></p> <script> document.getElementById("absolute").innerHTML = Math.abs(-5.13); </script> </body> </html>
Output
5.13
- Related Questions & Answers
- Math.Abs() Method in C#
- What is the use of forEach() method in JavaScript?
- What is the use of Math.pow() method in JavaScript?
- What is the use of substr() method in JavaScript?
- what is the use of slice() method in JavaScript?
- What is the use of Array.entries() method in JavaScript?
- What is the use of charAt() method in JavaScript?
- What is the use of Array.Reduce() method in JavaScript?
- What is the use of Array.Some() method in JavaScript?
- What is the use of Array.Find() method in JavaScript?
- What is the use of Array.findIndex() method in JavaScript?
- What is the use of Object.is() method in JavaScript?
- What is the use of Atomics.store() method in JavaScript?
- What is the use of Object.isFrozen() method in JavaScript?
- What is the use of weakSet.has() method in JavaScript?
Advertisements