admin管理员组文章数量:1336213
I was curious to know if there is a way in Javascript or JQuery to make a variable equal the lowest of a set of values simply.
So assuming I have:
X = 1
Y = 2
Var Z = lowest of X or Y
I know I could do an if statement that basically reads
if(X < Y){
Z = X
} else {
Z = Y
}
I was mainly just curious if something existed to do this in one line.
Thanks!
I was curious to know if there is a way in Javascript or JQuery to make a variable equal the lowest of a set of values simply.
So assuming I have:
X = 1
Y = 2
Var Z = lowest of X or Y
I know I could do an if statement that basically reads
if(X < Y){
Z = X
} else {
Z = Y
}
I was mainly just curious if something existed to do this in one line.
Thanks!
Share Improve this question edited Aug 23, 2017 at 12:45 Mihai Alexandru-Ionut 48.4k14 gold badges105 silver badges132 bronze badges asked Aug 23, 2017 at 9:17 Farrell ColemanFarrell Coleman 793 silver badges16 bronze badges 1- I need to ask harder questions :p I get too many correct answers and don't know who to mark as correct lol – Farrell Coleman Commented Aug 23, 2017 at 9:19
8 Answers
Reset to default 8Use Math.min
function: Math.min(X, Y)
You should use ternary
operator.
let z = x < y ? x : y
Another method is to use Math.min
function.
let z = Math.min(x, y);
You can use a ternary operator.
let X = 1;
let Y = 2;
let Z = (X<Y?X:Y);
console.log(Z);
this is what you need:
var Z = X < Y ? X : Y;
You can use,
X = 1;
Y = 2;
Z = Math.min(X, Y);
or
X = 1;
Y = 2;
Z = (X < Y) ? X : Y;
var X = 1;
var Y = 2;
document.write(Math.min(X,Y));
Try to use this:
let Z = Math.min(X, Y);
you could do this:
var z = x < y ? x : y
本文标签: javascriptAssign lowest of two valuesStack Overflow
版权声明:本文标题:javascript - Assign lowest of two values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742387051a2465243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论