Languages
[Edit]
EN

JavaScript - Math.abs() method example

4 points
Created by:
MindOfMadness3
696

The Math.abs() function returns the absolute value of a number. 

The absolute value of the number x denoted |x|, is the non-negative value of x without regard to its sign.

Popular usage examples:

Other usage examples:

1. Documentation

Edit
SyntaxMath.abs(number)
Parametersnumber - integer or float number value (primitive value).
ResultAbsolute value of a number (primitive value).
Description

Math.abs()  is a static method that takes only one parameter and returns absolute value. The absolute value of the number x denoted |x|, is the non-negative value of x without regard to its sign.

2. Math.abs() custom implementation

Edit
Note: there are few ways to change sign to opposite if (x < 0) is true:
  • x = -x
  • x *= -1
  • x = ~x + 1

Read this article to know about best way of negate operation.

3. Canvas plot example

Edit

References

Edit
  1. Absolute value - Wikipedia

Alternative titles

  1. JavaScript - Math.abs() documentation with examples
  2. js - Math.abs() method documentation with examples
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - Math.abs()

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join