javascript运算符
The conditional ternary operator in JavaScript assigns a value to a variable based on some condition and is the only JavaScript operator that takes three operands.
JavaScript中的条件三元运算符会根据某些条件为变量分配值,并且是唯一采用三个操作数JavaScript运算符。
The ternary operator is a substitute for an if statement in which both the if and else clauses assign different values to the same field, like so:
三元运算符可以替代if语句,在该语句中, if和else子句都将不同的值分配给同一字段,例如:
if (condition)
result = 'something';
else
result = 'som
The ternary operator shortens this if/else statement into a single statement:
三元运算符将此if / else语句缩短为一个语句:
result = (condition) ? 'something' : 'som
If condition is true, the ternary operator returns the value of the first expression; otherwise, it returns the value of the second expression. Let's consider its parts:
如果条件为true,则三元运算符返回第一个表达式的值;否则为false。 否则,它返回第二个表达式的值。 让我们考虑它的部分:
First, create the variable to which you want to assign a value, in this case, result. The variable result will have a different value depending on the condition.
首先,创建要为其分配值的变量,在这种情况下为result 。 根据条件,可变结果将具有不同的值。
Note that on the right-hand side (i.e. the operator itself), the condition is first.
请注意,在右侧(即操作员本身), 条件是第一个。
The condition is always followed by a question mark (?), which can basically be read as "was that true?"
条件始终后面带有问号( ? ),基本上可以将其理解为“是真的吗?”
The two possible results come last, separated by a colon (:).
两种可能的结果终于出来,分离由冒号(:))。
This use of the ternary operator is available only when the original if statement follows the format shown above — but this is quite a common scenario, and using the ternary operator can be far more efficient.
仅当原始if语句遵循上面显示的格式时,才可以使用三元运算符 -但这是很常见的情况,并且使用三元运算符会更加高效。
Let's look at a real example.
让我们看一个真实的例子。
Perhaps you need to determine which children are the right age to attend kindergarten. You might have a conditional statement like this:
也许您需要确定哪些孩子是上幼儿园的合适年龄。 您可能有如下条件语句:
var age = 7;
var kinder
if (age > 5) {
kindergarten_eligible = "Old enough";
}
else {
kindergarten_eligible = "Too yo
Using the ternary operator, you could shorten the expression to:
使用三元运算符,可以将表达式缩短为:
var kindergarten_eligible = (age < 5) ? "Too young" : "
This example would, of course, return "Old enough."
当然,此示例将返回“足够旧”。
Multiple Evaluations
多重评估
You can include multiple evaluations, as well:
您还可以包括多个评估:
var age = 7, var socially_ready = true;
var kindergarten_eligible = (age < 5) ? "Too young" : socially_ready
"Old enough but not yet ready" "Old and socially mature enough"
console.log ( kindergarten_eligible ); // logs "Old and socially ma
Multiple Operations
多种操作
The ternary operator also allows the inclusion of multiple operations for each expression, separated by a comma:
三元运算符还允许每个表达式包含多个运算符,并用逗号分隔:
var age = 7, socially_
age > 5 ? (
alert("You are old enough."),
location.assign("continue.html")
) : (
socially_ready = false,
alert("Sorry, but you are not yet rea
Ternary operators avoid otherwise verbose code, so on the one hand, they appear desirable. On the other hand, they can compromise readability — obviously, "IF ELSE" is more easily understood than a cryptic "?".
三元运算符避免了其他冗长的代码 ,因此,一方面,它们看起来是可取的。 另一方面,它们会损害可读性-显然,“ IF ELSE”比隐晦的“?”更容易理解。
When using a ternary operator — or any abbreviation — consider who will be reading your code. If less-experienced developers may need to understand your program logic, perhaps the use of the ternary operator should be avoided. This is especially true if your condition and evaluations are complex enough that you would need to nest or chain your ternary operator. In fact, these kinds of nested operators can impact not only readability but debugging.
使用三元运算符(或任何缩写)时,请考虑谁将读取您的代码。 如果经验不足的开发人员可能需要了解您的程序逻辑,则应避免使用三元运算符。 如果条件和评估非常复杂,以至于需要嵌套或链接三元运算符,则尤其如此。 实际上,这些嵌套的运算符不仅会影响可读性,还会影响调试。
As with any programming decision, be sure to consider context and usability before using a ternary operator.
与任何编程决策一样,在使用三元运算符之前,请务必考虑上下文和可用性。
翻译自: https://www.thoughtco.com/javascript-by-example-use-of-the-ternary-operator-2037394
javascript运算符