[英]How can I use javascript to get the highest value of two variables
我有兩個名為var h1
和var h2
變量,每個變量都包含一些數字。 我想獲得另一個變量的最高價值
嘗試此操作以獲取最大數量:
var result = Math.max(h1,h2)
var highestNum = h1 > h2 ? h1: h2;
或者,如果您不喜歡第三次陳述:
var highestNum;
if (h1 > h2) {
highestNum = h1;
} else {
highestNum = h2;
}
編輯:有趣的是,它似乎if-else語句比Math.max(在Chrome 21上)運行快得多
h1>h2?h1:h2
為什么需要jQuery?
花式的方法 應該做的唯一方法 :
var otherVar = Math.max(h1,h2);
很長的路要走 :
var otherVar=0;
if(h1 >= h2)
{
otherVar = h1;
}
else
{
otherVar = h2;
}
僅Javascript,無jQuery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.