javascript运算符_JavaScript中的if-else快捷方式:三元运算符

javascript运算符

if-else decisions are so common in coding languages that most have some sort of shortcut to create them. In JavaScript, this shortcut is referred to as a ternary, and is the only operator that uses two symbols.

if-else决策在编码语言中非常普遍,以至于大多数情况下都有创建它们的捷径。 在JavaScript中 ,此快捷方式称为ternary ,并且是唯一使用两个符号的运算符。

The ternary operator is not merely a shortcut: it is an expression that returns a value, making it extremely powerful in the right hands.

三元运算符不仅是捷径,它是一个返回表达式 ,使它在右手中极为强大。

“Ternary” means “composed of three parts” and refers to the three operands of the expression, separated by the two symbols of the operator:

“三元”是指“由三部分组成”,是指表达式的三个操作数,由运算符的两个符号分隔:

condition ? expression1 : expression2

The condition is any expression that will evaluate to true or false. If true, the expression immediately after the ? will be executed; if false, the expression after the : will be run instead. The result of this expression often becomes the value of a variable.

condition是任何计算结果为truefalse表达式。 如果为true ,则表达式后紧接? 将被执行; 如果为false ,则将改为运行:之后的表达式。 该表达式的结果通常成为变量的值。

Ternaries - also known as conditional operators - are often used where classical if else constructions would be awkward. Ternaries also have the advantage of being considerably more succinct:

三元数-也称为条件运算符-通常用于经典结构( if else结构不方便)。 三元组还具有简洁得多的优点:

var now = new Date(),
greeting = "Good" + ((now.getHours() >= 18) ? " evening." : " day.");
console.log(greeting);

The above code will write “Good day” to the console unless it is after 6pm, in which case “Good evening” will be written instead.

除非在下午6点之后,否则上述代码会将“ Good day”写入控制台 ,在这种情况下,将改为写入“ Good night”。

Another, more complex example, from my JavaScript randomness recipes article:

来自我的JavaScript随机性食谱文章的另一个更复杂的示例:

function coinFlip() {
    return (Math.floor(Math.random() * 2) === 0) ? "up" : "down";
}

If the number provided by the expression on the left is 0, the word up is returned; otherwise, the word down is provided instead.

如果左侧表达式提供的数字为0 ,则返回单词up ;否则返回false。 否则,将改为提供单词down

捷径 (A Shorter Shortcut)

The ternary operator must feature all three operands; a shortcut for if without an else, doesn’t formally exist. However, it is possible to structure something close by using an equality comparison operator, a logical AND operator and parentheses:

三元运算符必须具有所有三个操作数 ; if没有else的快捷方式将不存在。 但是,可以通过使用相等比较运算符逻辑AND运算符和括号来构造一些接近的东西:

var highest,
chosenMountain = "Katmandu";
chosenMountain == "Everest" && (highest = true);

If chosenMountain is “Everest”, the highest variable will be set to true; otherwise, it will be false (the default).

如果chosenMountain为“ Everest”,则highest变量将设置为true ; 否则,它将为false (默认值)。

必须使用三元运算符吗? (Must You Use the Ternary Operator?)

Just like other shortcuts in JavaScript, using the ternary operator is almost entirely optional: it works far better in many circumstances than if else, but it doesn’t have to replace it. In most cases, the same evaluation can be written either way in your code; the ternary operator is simply more efficient.

就像在JavaScript中其他快捷方式 ,使用三元操作几乎完全是可选的:它的工作原理好得多在许多情况下比if else ,但它并没有取代它。 在大多数情况下,可以在您的代码中以任何一种方式编写相同的评估。 三元运算符效率更高

翻译自: https://thenewcode.com/416/An-if-else-shortcut-in-JavaScript-the-ternary-operator

javascript运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值