(In case you’re a little rusty on your math, the absolute value of a number is just that number’s value without a sign; in practice, it comes down to removing the minus sign from a negative number.)
Meet the Math Object
Here’s how it works. One of the easiest ways to use a Math object method is to set a variable to equal the method, like this:
When you do that, the value of Alice becomes the value that’s returned by some_method operating on some_number.
The absolute value method is just called abs(), so if you want Alice to be the absolute value of -2,743, you’d use this:
The value of Alice would then be 2,743, without the minus sign.
And of course, since it’s an absolute value, if you use the absolute value function with a positive number, the sign doesn’t change:
The value of Alice is now 326.
What Does It Take?
So, what kinds of arguments will abs() take? Obviously, it will take positive and negative numbers, but it will also take a variety of other types of argument.
For example, it will take an ordinary arithmetic operation as an argument:
var Alice=Math.abs(17.536 - 233);
This sets the value of Alice to 215.464 — the absolute value of 17.536 – 233 = -215.464.
It will also take other Math objects; the Math object in the argument gets evaluated first, then abs() returns the absolute value:
The value of Alice is now set to 0.6536436208636119 — the absolute value of the cosine of 4 (which is -0.6536436208636119).
And Variables, Too!
And of course, it will take a variable as an argument. if you first set
var Bob=(-27 * 35);
then take the absolute value of Bob
The value of Alice is now 945 — the absolute value of -27 * 35 = 945.
This returns 0, since null = no value.
Not a Number?!?
But what happens if you give abs() an argument that isn’t a number, or anything that returns a number, or null, such as a string?
var Alice=Math.abs("Don't look at me - I'm just a string!");
var Alice=Math.abs(5 + Math.sqrt(-1));
it will set Alice to NaN, rather than trying to evaluate the complex number.
Putting abs() To Use
In practice, of course, you’re probably not going to want to hard-code a number just to get its absolute value, so something like this:
really isn’t very useful.
There are, however, situations where being able to get an absolute value can be useful. If, for example, you’re designating numbered positions on a line with zero at the midpoint, anything to the left of zero is going to have a negative coordinate. And if you’re dealing with two-dimensional coordinates (such as positions in a graphic image or a table), you can encounter negative coordinates.
How Far Is Minus Five Miles?
Under those conditions, how do you find the distance between any two points? You can always check the signs of their coordinates and do the appropriate arithmetic, of course, but a much more general solution is just to subtract one coordinate from another, and take the absolute value of the result. Consider a simple two-dimensional example:
Jim’s standing at one point on a numbered line with 0 at the midpoint, and Jeff is standing at another point. How far apart are they? if Jim is at position 5 and Jeff is at -3, their distance is the absolute value of the difference between the two numbers.
var Alice=Math.abs(-3 - 5);
var Alice=Math.abs(5 - -3);
both return a value of 8 for Alice.
And if Jim and Jeff are on the same side of the midpoint, so Jim is at 5 and Jeff is at 3 — or if Jim is at -5 and Jeff is at -3 — it also works:
var Alice=Math.abs(3 - 5); var Alice=Math.abs(5 - 3); var Alice=Math.abs(-3 - -5); var Alice=Math.abs(-5 - -3);
all set the value of Alice to 2.
It’s a Useful Tool