Math.abs() Method in JavaScript


Written by - Olorunfemi Akinlua
Reviewed by - Deepak Prasad

Introduction to Math.abs() JavScript Method

With JavaScript, we can carry out mathematical operations faster, and as a language, it provides many ways to work with numbers. There are many operators, properties, methods, and objects that help with mathematics in JavaScript.

A very important one is the Math object, and within it are static properties and methods that are useful for performing mathematical operations. One such static method is the math.abs method.

In this article, we will discuss the Math object for some context and the Math.abs method to deal with absolute values.

 

The Math Object

Everything in JavaScript is an object. The Math object is a global object (and is not a constructor or function object) that contains properties and methods that work with only the Number type.

It provides mathematical constants such as pi and Euler’s constants among others, and operations such as cosine, sine, or absolute value of a number. The static properties of the Math object provides the mathematical constants and the static methods of the Math object.

So, for this article, the Math.abs method provides the absolute value of a number it's called upon.

 

Use Math.abs() JavaScript Method

The static method, Math.abs(), returns the absolute value of a number passed to it. So if a positive number is passed to the Math.abs() method, it returns that number, but if you pass a negative number (a number lower than zero), it will return the positive number of that number.

Let’s illustrate the Math.abs() method quickly by passing some numbers.

console.log(Math.abs(-23));
console.log(Math.abs(45));

Output

23
45

But what’s amazing is that it can coerce its parameter (type coercion), and so if you pass a string that contains a number, it will return the absolute value of that number. In addition, certain values are coerced to numeric values but some values are not coercible such as NaN.

console.log(Math.abs("-23"));
console.log(Math.abs(true));
console.log(Math.abs(false));
console.log(Math.abs([]));

Output

23
1
0
0

A good example of how to use the Math.abs() method is calculating differences. If we are to calculate the difference, we can do it via the code below

function diff(a, b) {
    if (a - b < 0) {
        return b - a;
    }
    return a - b;
}

console.log(diff(3, 2));
console.log(diff(2, 3));

Output

1
1

But we could have implemented the same function using the Math.abs() method

function diff(a, b) {
    return Math.abs(a - b);
}

console.log(diff(3, 2));
console.log(diff(2, 3));

Output

1
1

 

Summary

The Math.abs() method allows us to get the absolute value of a number, and can come useful in certain mathematical operations we carry out in JavaScript.

 

References

Math - JavaScript | MDN (mozilla.org)
Math.abs() - JavaScript | MDN (mozilla.org)
Number - JavaScript | MDN (mozilla.org)

 

Views: 0

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on LinkedIn.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment