JavaScript - Mathematical module of a value

4

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

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

    
asked by anonymous 21.11.2017 / 18:04

2 answers

5

This |n| syntax does not exist in JavaScript, at least for this purpose.

What you should 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
  

Note: 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)) . O   first returns 4 and second 16 .

More about Math.abs () in MDN .

    
21.11.2017 / 18:33
6

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

console.log(Math.abs(10));
console.log(Math.abs(-10));
    
21.11.2017 / 18:33