Maths Method in JavaScript

Hello folks, i’m a beginner in JavaScript. Please can someone throw some lights on Absolute Number(Maths Method). Also this two line of codes gives same result:

var a = 6;
console.log("absolute value of a is " + Math.abs(a) + “.”); //absolute value of a is 6

and
var a = 6;
console.log("absolute value of a is " + a + “.”); // absolute value of a is 6

my question now is , what is the function of Math.abs() in the first line of code if i can just insert variable a and still get the same result

Try with minus sign before value and you’ll see the difference.
var a = -6;
Math.abs(a)
console.log("absolute value of a is " + Math.abs(a) + “.”); //absolute value of a is 6


var a = -6;
console.log("absolute value of a is " + a + “.”); // absolute value of a is -6

Absolute values removes sign. Negative becomes positive, and positive becomes positive.

1 Like

Thank you boss, well explained.