08 Aug, 2023
Hey, Fuelers! JavaScript has a built-in function to take the absolute value of a number, called Math.abs()
. As a human programmer(yes, chatGPT can code too, so just want to be specific😉), Math.abs()
is one of those functions you'll find yourself using again and again.
In this article, I'm going to show you exactly how Math.abs()
works, give you a few examples of how you can use it in your code, and share some tips to help you understand the absolute value in JavaScript.
Let's dive in!
So what exactly is absolute value? In simple terms, the absolute value of a number is its distance from zero on the number line. It's always a positive value, even if the original number is negative.
For example, the absolute value of 5 is 5, because 5 is 5 units from 0. The absolute value of -5 is also 5, because -5 is 5 units from 0, just in the opposite direction.
In mathematical notation, the absolute value of a number x is written as |x|.
So |5| = 5 and |-5| = 5.
To find the absolute value of a number in JavaScript, you can use the Math.abs() function.
For example:
Math.abs(5); // Returns 5
Math.abs(-5); // Also returns 5
Math.abs(0); // Returns 0
Math.abs()
will return the absolute value of any number you pass to it, whether positive, negative, or zero.
Absolute value comes in handy when you want to calculate the distance between two points on a number line, or the magnitude of a velocity or acceleration, regardless of direction.
For example, if a car is traveling at 50 miles per hour north, and then turns around and travels 50 miles per hour south, its total distance traveled is 100 miles, even though its velocity changed directions. The absolute values of the velocities (50 mph) can be added to get the total distance.
Math.abs()
JavaScript?So you want to use absolute value in your JavaScript code, huh?
Math.abs()
is the function you need. It gives you the absolute value of any number you pass to it.
Say you have a number like -5. If you pass that to Math.abs()
, it will return 5. The negative is stripped away, leaving just the positive value.
Math.abs(-5); // Returns 5
Math.abs(0); // Returns 0
Math.abs(10); // Returns 10
This works for both integers and decimals. Math.abs()
will return the positive version of any negative number you give it.
Maybe you want to find the difference between two numbers, but you don't care which one is larger. Math.abs()
have you covered there too?
let difference = Math.abs(10 - 15); // Returns 5
It works on variables as well. If you have a number stored in a variable, just pass that variable to Math.abs().
let num = -20;
let positive = Math.abs(num); // positive is now 20
You can use Math.abs() in if statements to check if a number is above a certain threshold without worrying about its sign.
let myNum = -15;
if (Math.abs(myNum) > 10) {
console.log('Number is greater than 10'); // Prints to console
}
So go forth and absolutely value to your heart's content! Math.abs()
is a simple but useful tool to have in your JavaScript toolbox.
Here are a few more examples to illustrate how Math.abs()
works:
Math.abs(-10); // Returns 10
Math.abs(10.5); // Returns 10.5
Math.abs(-4.81); // Returns 4.81
Math.abs(0.0001);// Returns 0.0001
You can also use Math.abs()
on integer and float values. It works the same in all cases - by stripping the sign and returning the magnitude.
Math.abs
Use casesAbsolute value is useful in a lot of scenarios. Some common use cases are:
Math.abs()
So you want to use Math.abs()
in JavaScript but have some questions about how it works. No worries, we’ve got you covered. Here are the most common FAQs about Math.abs()
:
Math.abs()
do?Math.abs()
returns the absolute value of a number. That means it returns a number’s positive value. So if you pass it -5, it will return 5. If you pass it 5, it will also return 5.
Math.abs()
work on decimals or just integers?Math.abs()
works on both decimals and integers in JavaScript. You can pass it values like -1.5 and it will return 1.5.
Math.abs()
on strings or arrays?No, Math.abs()
only works on numbers (ints and floats). If you pass it a string, array, or any other non-number value, you'll get a NaN (Not a Number) result.
Nope! Math.abs() is built into the JavaScript Math object, so you can call it directly. All you need is:
Math.abs(number)
Replace the number with the actual number you want to get the absolute value of.
Math.abs()
?There are a couple of other options for getting a number's absolute value in JS:
But Math.abs()
is cleaner and easier to read, so I would recommend sticking with that.
So, what's the big takeaway here? Absolute value in JavaScript basically gives you the magnitude of a number, without considering whether it's positive or negative. It's the distance from zero on the number line.
Some key points to remember:
Math.abs()
to get the absolute value of a number.For example:
Math.abs(5); // Returns 5
Math.abs(-5); // Also returns 5
Math.abs(3.14); // Returns 3.14
Math.abs(-2.72); // Returns 2.72
You can think of it like this: Math.abs() gives you the size of a number, without its sign.
Absolute value comes in handy when you want to calculate a distance between two points, get the magnitude of a velocity or acceleration, or in any case where the direction doesn't matter, only the quantity.
So there you have it, a quick and easy explanation of JavaScript's Math.abs()
function. Practice makes perfect.
Keep learning, keep building.
Must Reads in JavaScript:
Trusted by 49900+ Generalists. Try it now, free to use
Start making more money