I have two variables name var h1 and var h2
each holds some numbers.
i want to get highest value to another variable
Try this to get the maximum:
var result = Math.max(h1,h2)
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)
http://jsperf.com/jbabey-math-max
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With