使用 if 语句设置 javascript 变量——“var = x”应该在 IF 内部还是外部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31971801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
setting a javascript variable with an if statement -- should the 'var = x' be inside or outside the IF?
提问by user45867
This may be a basic question but I'm having difficulty finding an answer.
这可能是一个基本问题,但我很难找到答案。
You want to set var B based on var A
您想根据 var A 设置 var B
would you do
你会怎么做
var B = if(A == "red"){"hot"}else{"cool"}
I don't think this works.
我不认为这行得通。
I guess you could do
我想你可以做到
if(A == "red"){var B = "hot"}else{var B = "cool"}
This doesn't seem particularly elegant. I mean I would prefer something that starts with var b = .... just for clarity's sake.
这似乎不是特别优雅。我的意思是我更喜欢以 var b = .... 开头的东西,只是为了清楚起见。
回答by James LeClair
perfect use for a ternary
完美使用三元
var B = (A ==="red") ? "hot":"cool";
Ternary expressions will always return the first value if true, the second value if not. Great for one-off if/else statements, but if you get into more nested conditions, be sure to use the traditional if/else blocks for readability.
如果为真,三元表达式将始终返回第一个值,否则返回第二个值。非常适合一次性 if/else 语句,但如果您遇到更多嵌套条件,请务必使用传统的 if/else 块以提高可读性。
回答by StephenTG
While you could use an if statement for this, you would probably be best off using the ternary operator. In the example you describe, you'd set it up as:
虽然您可以为此使用 if 语句,但最好使用三元运算符。在您描述的示例中,您将其设置为:
var B = A === "red" ? "hot" : "cool";
The ternary operator in general works as follows:
三元运算符的工作原理如下:
someBooleanExpression ? resultIfSomeBooleanExpressionIsTrue : resultIfSomeBooleanExpressionIsFalse;
Also note the use of ===
to check comparison, rather than a single =
, which is assignment. ==
could also be used, but might end up being less accurate
还要注意使用===
来检查比较,而不是单个=
,这是赋值。==
也可以使用,但最终可能不太准确
回答by nilesh93
Initialize your variables first, then compare using if statement and assign new values where necessary
首先初始化变量,然后使用 if 语句进行比较并在必要时分配新值
var a = "red";
var b;
if(a=="red"){
b="hot";
}
else{
b="cold";
}
回答by Rune FS
In your core you are declaring an initializing a to "red"
which is truthy so the else clause will never be used. In JavaScript it's an if-statementnot an if-expressionas in say F#. That means you are not initializing B.
在您的核心中,您声明了一个初始化"red"
为真的a,因此永远不会使用 else 子句。在 JavaScript 中,它是一个 if-语句,而不是像 F# 中的 if-表达式。这意味着您没有初始化 B.
What you are probably looking for is
您可能正在寻找的是
var B = A ==="red" ? "hot" : "cool"
That is to say use the conditionaloperator (*) to initialize B to "hot"
if A is a string that equals "red"
otherwise to "cool"
也就是说,如果 A 是一个字符串,则使用条件运算符 (*) 将 B 初始化为"hot"
"red"
"cool"
(*) The operator is sometimes imprecisely called the ternary operator or more mistakenly the tertiary. It isn't so bad to call it the ternary operator as long as there's never added another ternary operator, at which point it would be ambiguous
(*) 运算符有时被不准确地称为三元运算符或更错误地称为三元运算符。只要从来没有添加另一个三元运算符,将其称为三元运算符也不错,此时它会模棱两可
回答by heytools
First option
第一个选项
var A = "blue";
var B = (A == "red") ? "hot" : "cool";
Second option
第二种选择
var A = "blue";
var B = ""; // Or simply: var B;
if (A == "red") B = "hot"; else B = "cool";