你如何使用? JavaScript 中的 : (条件)运算符?
什么是 ?:
(问号和冒号运算符,又名条件或“三元”)运算符以及如何使用它?
What is the ?:
(question mark and colon operator aka. conditional or "ternary") operator and how can I use it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
这是 if-else 语句的单行简写。它称为条件运算符。1
下面是可以使用条件运算符缩短的代码示例:
可以使用
?:
缩短代码,如下所示:像所有表达式一样,条件运算符也可以用作具有副作用的独立语句,尽管这是 在缩小之外不寻常:
它们甚至可以链接起来:
但是要小心,否则你最终会得到像这样的复杂代码:
1 通常被称为“三元运算符”,但实际上它只是一个三元运算符[接受三个操作数的运算符]。不过,它是 JavaScript 目前唯一拥有的一种。
This is a one-line shorthand for an if-else statement. It's called the conditional operator.1
Here is an example of code that could be shortened with the conditional operator:
This can be shortened with the
?:
like so:Like all expressions, the conditional operator can also be used as a standalone statement with side-effects, though this is unusual outside of minification:
They can even be chained:
Be careful, though, or you will end up with convoluted code like this:
1 Often called "the ternary operator," but in fact it's just a ternary operator [an operator accepting three operands]. It's the only one JavaScript currently has, though.
我想在给定的答案中添加一些内容。
如果您在“如果已设置则显示变量,否则...”之类的情况下遇到(或想要使用)三元,您可以使其更短,没有三元。
而不是:
您可以使用:
这是 PHP 速记三元运算符
?:
的 Javascript 等效项甚至:
它评估变量,如果它为 false 或未设置,则继续到下一个。
I want to add some to the given answers.
In case you encounter (or want to use) a ternary in a situation like 'display a variable if it's set, else...', you can make it even shorter, without a ternary.
Instead of:
You can use:
This is Javascripts equivallent of PHP's shorthand ternary operator
?:
Or even:
It evaluates the variable, and if it's false or unset, it goes on to the next.
它称为“三元”或“条件”运算符。
示例
来自 MSDN JS 文档。
基本上它是一个简写的条件语句。
另请参阅:
It's called the 'ternary' or 'conditional' operator.
Example
From MSDN JS documentation.
Basically it's a shorthand conditional statement.
Also see:
当你只有符号时,用谷歌搜索有点困难;)使用的术语是“JavaScript 条件运算符”。
如果您在 JavaScript 中看到更多有趣的符号,您应该首先尝试查找 JavaScript 的运算符: Mozilla Developer中心运营商名单。您可能遇到的一个例外是
$
符号 。要回答您的问题,请用条件运算符替换简单的 if 语句。最好举个例子:
而不是:
It's a little hard to google when all you have are symbols ;) The terms to use are "JavaScript conditional operator".
If you see any more funny symbols in JavaScript, you should try looking up JavaScript's operators first: Mozilla Developer Center's list of operators. The one exception you're likely to encounter is the
$
symbol.To answer your question, conditional operators replace simple if statements. An example is best:
Instead of:
大多数答案都是正确的,但我想补充一点。 三元运算符是右关联的,这意味着它可以通过以下方式链接
if … else-if … else-if … else
:相当于:
更多详细信息位于此处
Most of the answers are correct but I want to add little more. The ternary operator is right-associative, which means it can be chained in the following way
if … else-if … else-if … else
:Equivalent to:
More details is here
相当于
除了,当然,它更短。
is equivalent to
except, of course, it's shorter.
三元运算符
在 JavaScript 中,我们通常有条件语句。
示例:
但它包含两行或更多行,并且不能分配给变量。
Javascript 对于这个问题有一个解决方案三元运算符。
三元运算符可以写在一行中并分配给一个变量。
示例
这个三元运算符与 C 编程语言中的类似。
Ternary Operator
Commonly we have conditional statements in Javascript.
Example:
but it contain two or more lines and cannot assign to a variable.
Javascript have a solution for this Problem Ternary Operator.
Ternary Operator can write in one line and assign to a variable.
Example:
This Ternary operator is Similar in C programming language.
它被称为三元运算符
It is called the ternary operator
嘿伙计,记住 js 的工作原理是评估 true 或 false,对吧?
让我们采用三元运算符:
首先,js 检查 QuestionAnswered 是
true
还是false
。如果
true
(?
) 你会得到“Awesome!”否则(
:
)你会得到“damn”;希望这对朋友有帮助:)
Hey mate just remember js works by evaluating to either true or false, right?
let's take a ternary operator :
First, js checks whether questionAnswered is
true
orfalse
.if
true
(?
) you will get "Awesome!"else (
:
) you will get "damn";Hope this helps friend :)
三元表达式在 JS 中非常有用,尤其是 React。这是对所提供的许多好的、详细的答案的简化答案。
将
expressionIfTrue
视为呈现 true 的 OG if 语句;将
expressionIfFalse
视为 else 语句。示例:
检查 x 的值,如果 true,则返回第一个 y=(value),如果 false,则返回冒号后的第二个 y=(value)。
Ternary expressions are very useful in JS, especially React. Here's a simplified answer to the many good, detailed ones provided.
Think of
expressionIfTrue
as the OG if statement rendering true;think of
expressionIfFalse
as the else statement.Example:
this checked the value of x, the first y=(value) returned if true, the second return after the colon : returned y=(value) if false.
一元
二进制
三元
unary
Binary
Ternary
这是一个
if 语句
,全部在一行中。所以
要求值的表达式在
( )
中,如果匹配true,则执行
后面的代码?
如果匹配false,则执行
后面的代码:
It's an
if statement
all on one line.So
The expression to be evaluated is in the
( )
If it matches true, execute the code after the
?
If it matches false, execute the code after the
:
如需更多说明,请阅读 MDN 文档链接
For more clarification please read MDN document link
我们可以使用 Jquery 以及长度,如下例所示:
假设我们有 GuarantorName 文本框,它具有值并且想要获取名字和姓氏 - 它可能为空。
所以rathar比
我们可以使用下面的代码与Jquery以最少的代码
We can use with Jquery as well as length as below example :
Suppose we have GuarantorName textbox which has value and want to get firstname and lastname- it may be null.
So rathar than
We can use below code with Jquery with minimum code
利用三元运算符,编写一个程序,如果数字是偶数,则打印“偶数”;如果数字是奇数,则打印“奇数”。
输出:13是奇数
By using Ternary operator, write a program to Print “Even Number”, if the number is even or Print “Odd Number”, if the number is odd.
Output : 13 is Odd number
它称为
三元运算符
。有关更多信息,这是我回答的另一个问题:如何编写不带“else”的 IF else 语句
It's called the
ternary operator
. For some more info, here's another question I answered regarding this:How to write an IF else statement without 'else'
这可能不是最优雅的方法。但对于不熟悉三元运算符的人来说,这可能很有用。我个人的偏好是使用 1 行后备而不是条件块。
三元运算符
This is probably not exactly the most elegant way to do this. But for someone who is not familiar with ternary operators, this could prove useful. My personal preference is to do 1-liner fallbacks instead of condition-blocks.
Ternary Operator
如果您在 javascript 中有一个条件检查实例函数。使用三元运算符很容易。只需一行即可实现。
例如:
像这样具有一个条件的函数可以写如下。
条件?如果为真:如果为假
If you have one condition check instance function in javascript. it's easy to use ternary operator. which will only need one single line to implement.
Ex:
a function like this with one condition can be written as follow.
condition ? if True : if False
三元运算符只是编写 if else 条件的一种简单方法。它在ReactJS中被广泛使用。
例如:
Ternary operator is just a simple way to write if else condition. It is widely used in ReactJS.
For Example: