Determine Minimum and Maximum
By Stephen Bucaro
If you want to compare two numbers in Java Script, you can always use an if-else
statement, but the Math object provides two methods that do that for you. The
Math.max method returns the highest value, the Math.min method returns
the lowest value.
var a = 5;
var b = 6;
var n = Math.max(a,b);
The code shown above declares two variables a, assigned as value of 5, and b, assigned
as value of 6. When both variables are passed to Math.max method, it returns the
value of the variable with the highest value (6).
var a = 5;
var b = 6;
var n = Math.min(a,b);
Similarly, When both variables are passed to Math.min method, it returns the
value of the variable with the lowest value (5).
When you need to compare two numbers use the apropriate Java Script Math object
max or min method.
More Java Script Code: • Java Script Reserved Words • Accessing Web Page Elements • Determine Absolute Value • The Java Script window.open Method • Determine Minimum and Maximum • Java Script Quick Reference : JavaScript toString Method Converts a Number to a String • Java Script Data Types • Java Script Arithmetic Operators • Extract Part of a String • JavaScript Operators
|