Javascript - Mathematical module of a value

Asked

Viewed 196 times

3

Is there a command (or symbol) that makes the mathematical module a value?

For example: var teste = |10|-|6|;

2 answers

5


That syntax |n| does not exist in Javascript, at least for this purpose.

What you’d have to do is use Math.abs(). Examples:

var teste = Math.abs(10)-Math.abs(6); // 4
var teste = Math.abs(10-6); // 4
var teste = Math.abs(-10-6); // 16
var teste = Math.abs(-10-(-6)) // 4

Obs.: When involving mathematical operations between values, use Math.abs() for each value. For example:

Math.abs(10) - Math.abs(-6) is different from Math.abs(10-(-6)). The first returns 4 and the second 16.

More about Math.abs() on MDN.

  • Understood how it works, thanks @Dvd, great explanation

5

From what I understand you want the module representation in a number. See Math.abs()

console.log(Math.abs(10));
console.log(Math.abs(-10));

  • Oh yes, I will study about this command, thank you very much !! Math.mod was giving error x.x

Browser other questions tagged

You are not signed in. Login or sign up in order to post.