Determine Absolute Value
By Stephen Bucaro
The absolute value is simply the value without a sign, for example the
absolute value of 4, or +4, is the same as the absolute value of -4: 4. If you
are creating an application that requires the absolute value of a number, use
the Java Script Math.abs method.
For example if you are designing a graphics application that lets the user
draw a horizontal line on the screen and you want to display the length of the
line in the applications status bar.
If the user draws a line from point a = 100 pixels to point b = 50 pixels,
we can subtract a - b to get the line length of 50 pixels. But what if the user
draws a line from point a = 50 pixels to point b = 100 pixels? Subtracting
a - b = -50 pixels. A line length of -50 pixels is a little confusing to the user.
We can solve that problem by passing the subtract statement to the Math.abs
method as shown below.
var length = Math.abs(a - b);
The Math.abs method will always return the result as an unsigned number.
More Java Script Code:
• Determine Minimum and Maximum
• Cookie Power Made Easy
• Convert a String to a Number
• The while Loop
• Java Script confirm Message Box
• Java Script Strings
• Format a Number as Currency
• Remove Blank Spaces From Ends of a String