How Can I Use Javascript To Get The Highest Value Of Two Variables
I have two variables name var h1 and var h2 each holds some numbers. i want to get highest value to another variable
Solution 1:
Try this to get the maximum:
var result = Math.max(h1,h2)
Solution 2:
var highestNum = h1 > h2 ? h1: h2;
or if you dislike tertiary statements:
var highestNum;
if (h1 > h2) {
highestNum = h1;
} else {
highestNum = h2;
}
Edit: Interestingly enough, it appears the if-else statement runs much faster than Math.max (on Chrome 21)
Solution 3:
Fancy way The only way it should be done :
var otherVar = Math.max(h1,h2);
Long way :
var otherVar=0;
if(h1 >= h2)
{
otherVar = h1;
}
else
{
otherVar = h2;
}
Javascript only, no jQuery.
Post a Comment for "How Can I Use Javascript To Get The Highest Value Of Two Variables"