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:
• Java Script Character Encoding and Decoding
• Convert a Number to a String
• Window onload Event
• The Browsers History Object
• Determine Absolute Value
• The while Loop
• Java Script Reserved Words
• Java Script Trigonometric Methods
• Interactively Set Webpage Colors
• Dueling Windows