称为“三元”或“条件”运算符。
有人可以用简单的语言向我解释什么是?:
(有条件的“三进制”)运算符,以及如何使用它?
这是if-else语句的单行缩写。它称为条件运算符。1
这里是可以用条件运算符缩短的代码示例:
var userType;
if (userIsYoungerThan18) {
userType = "Minor";
} else {
userType = "Adult";
}
if (userIsYoungerThan21) {
serveDrink("Grape Juice");
} else {
serveDrink("Wine");
}
可以用?:
缩短它,如下所示:
var userType = userIsYoungerThan18 ? "Minor" : "Adult";
serveDrink(userIsYoungerThan21 ? "Grape Juice" : "Wine");
[与所有表达式一样,条件运算符也可以用作具有副作用的独立语句,尽管这在缩小之外是unusual:
userIsYoungerThan21 ? serveGrapeJuice() : serveWine();
甚至可以将它们链接起来:
serveDrink(userIsYoungerThan4 ? 'Milk' : userIsYoungerThan21 ? 'Grape Juice' : 'Wine');
但是要小心,否则您将得到像这样复杂的代码:
var k = a ? (b ? (c ? d : e) : (d ? e : f)) : f ? (g ? h : i) : j;
1 通常称为“三元运算符”,但实际上它只是a三元运算符[一个接受三个操作数的运算符]。不过,它是目前仅有的一种JavaScript。
:
一元
x = 9 y = 8
2>3 ? true : false;
2<3 ? true : false;
2<3 ? "2 is lesser than 3" : "2 is greater than 3";
。有关更多信息,这是我回答的另一个问题:ternary operator
How to write an IF else statement without 'else'
// var firstName = 'John'; // Undefined
var lastName = 'Doe';
// if lastName or firstName is undefined, false, null or empty => fallback to empty string
lastName = lastName || '';
firstName = firstName || '';
var displayName = '';
// if lastName (or firstName) is undefined, false, null or empty
// displayName equals 'John' OR 'Doe'
// if lastName and firstName are not empty
// a space is inserted between the names
displayName = (!lastName || !firstName) ? firstName + lastName : firstName + ' ' + lastName;
// if display name is undefined, false, null or empty => fallback to 'Unnamed'
displayName = displayName || 'Unnamed';
console.log(displayName);
假设我们有GuarantorName文本框,该文本框具有值,并且想要获取名字和姓氏-它可以为null。比[
条件(三元)运算符是唯一的JavaScript运算符需要三个操作数。此运算符经常用作if语句的快捷方式。
将condition ? expressionIfTrue : expressionIfFalse
视为OG,如果陈述为真;将expressionIfTrue
视为else语句。
var x = 1;
(x == 1) ? y=x : y=z;
我要添加一些给定的答案。
如果在“显示已设置变量的情况下,否则,...”的情况下遇到(或想要使用)三元,则可以使其更短,无三元。
而不是:
var welcomeMessage = 'Hello ' + (username ? username : 'guest');
您可以使用:
var welcomeMessage = 'Hello ' + (username || 'guest');
这是PHP的速记三元运算符 甚至:?:
的Javascript等效项] >>
var welcomeMessage = 'Hello ' + (username || something || maybethis || 'guest');
它计算变量,如果它为假或未设置,则继续到下一个。
称为“三元”或“条件”运算符。
示例
?:运算符可以用作if ... else语句的快捷方式。它通常用作较大的表达式,如果...否则声明会很尴尬。对于例如:
var now = new Date(); var greeting = "Good" + ((now.getHours() > 17) ? " evening." : " day.");
该示例创建一个字符串包含“晚上好”。如果是晚上6点以后。等效代码使用if ... else语句看起来像如下:
var now = new Date(); var greeting = "Good"; if (now.getHours() > 17) greeting += " evening."; else greeting += " day.";
基本上是简写的条件语句。
另请参见:
当您所有的都是符号时,用Google进行搜索有点困难;)要使用的术语是“ javascript条件运算符”。
[如果您在Javascript中看到更多有趣的符号,则应先尝试查找Javascript的运算符:MDC's list of operators。您可能会遇到的一个例外是$
symbol。
要回答您的问题,条件运算符
替换简单的if语句。最好的例子是:$
而不是:
var insurancePremium = age > 21 ? 100 : 200;
var insurancePremium;
if (age > 21) {
insurancePremium = 100;
} else {
insurancePremium = 200;
}
相当于
z = (x == y ? 1 : 2);
当然,它更短。
三元运算符
通常我们在Java语言中有条件语句。
示例:
if (x == y) z = 1; else z = 2;
但它包含两行或更多行,因此无法分配给变量。Javascript为此问题三元运算符
提供了解决方案。三元运算符可以写在一行中并分配给一个变量。
示例:
if (true) { console.log(1) } else { console.log(0) } # Answer # 1
此三元运算符在C编程语言中相似。
称为三元运算符
var operator = true ? 1 : 0
console.log(operator)
# Answer
# 1
[队友们还记得js通过评估true或false对吗,对吧?
让我们使用三元运算符:
tmp = (foo==1 ? true : false);
首先,js检查questionAnswered是
questionAnswered ? "Awesome!" : "damn" ;
还是true
。如果
false
(true
),您将获得“超赞!”
否则(?
),您将得到“该死”;
希望这对朋友有帮助:)
大多数答案都是正确的,但我想补充一点。 三元运算符
:
:if … else-if … else-if … else
相当于:
function example() { return condition1 ? value1 : condition2 ? value2 : condition3 ? value3 : value4; }
更多详细信息是function example() {
if (condition1) { return value1; }
else if (condition2) { return value2; }
else if (condition3) { return value3; }
else { return value4; }
}
所以
if statement
要计算的表达式在var x=1; (x == 1) ? y="true" : y="false"; alert(y);
中如果匹配,则在
( )
之后执行代码>如果匹配为false,则在
?
之后执行代码>
:
一元
x = 9 y = 8
二进制
++x
--x
三元
z = x + y
称为2>3 ? true : false; 2<3 ? true : false; 2<3 ? "2 is lesser than 3" : "2 is greater than 3";
。有关更多信息,这是我回答的另一个问题:
ternary operator
这可能不是最完美的方法。但是对于不熟悉三元运算符的人,这可能会有用。我个人的喜好是执行1线备用广告,而不是使用条件限制。How to write an IF else statement without 'else'
// var firstName = 'John'; // Undefined var lastName = 'Doe'; // if lastName or firstName is undefined, false, null or empty => fallback to empty string lastName = lastName || ''; firstName = firstName || ''; var displayName = ''; // if lastName (or firstName) is undefined, false, null or empty // displayName equals 'John' OR 'Doe' // if lastName and firstName are not empty // a space is inserted between the names displayName = (!lastName || !firstName) ? firstName + lastName : firstName + ' ' + lastName; // if display name is undefined, false, null or empty => fallback to 'Unnamed' displayName = displayName || 'Unnamed'; console.log(displayName);
我们可以使用Jquery以及长度,如下例所示:假设我们有GuarantorName文本框,该文本框具有值,并且想要获取名字和姓氏-它可以为null。比[
我们可以将以下代码与带有最少代码的Jquery一起使用var gnamesplit = $("#txtGuarantorName").val().split(" "); var gLastName = ""; var gFirstName = ""; if(gnamesplit.length > 0 ){ gLastName = gnamesplit[0]; } if(gnamesplit.length > 1 ){ gFirstName = gnamesplit[1]; }
var gnamesplit = $("#txtGuarantorName").val().split(" "); var gLastName = gnamesplit.length > 0 ? gnamesplit[0] : ""; var gFirstName = gnamesplit.length > 1 ? gnamesplit[1] : ""; $("#txtLastName").val(gLastName); $("#txtFirstName").val(gFirstName);
条件(三元)运算符是唯一的JavaScript运算符需要三个操作数。此运算符经常用作if语句的快捷方式。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div > Guarantor Name: <input type="text" id="txtGuarantorName" value="ASP.NET Core" /><br/> <br/> <br/> First Name: <input type="text" id="txtLastName" value="ASP.NET Core" /> Last Name: <input type="text" id="txtFirstName" value="ASP.NET Core" /> </div>
如果条件为真,则运算符返回expr1的值;否则,它返回expr2的值。
condition ? expr1 : expr2
有关更多说明,请阅读function fact(n) { if (n > 1) { return n * fact(n-1); } else { return 1; } // we can replace the above code in a single line of code as below //return (n != 1) ? n * fact(n - 1) : 1; } console.log(fact(5));
三元表达式在JS中非常有用,尤其是React。这是对所提供的许多优质详细信息的简化答案。将condition ? expressionIfTrue : expressionIfFalse
视为OG,如果陈述为真;将expressionIfTrue
视为else语句。示例:
expressionIfFalse
这检查了x的值,如果为true,则返回第一个y =(value),如果为false,则返回第二个冒号:如果为false,则返回y =(value)。var x = 1; (x == 1) ? y=x : y=z;
称为“三元”或“条件”运算符。
当您所有的都是符号时,用Google进行搜索有点困难;)要使用的术语是“ javascript条件运算符”。
var insurancePremium;
if (age > 21) {
insurancePremium = 100;
} else {
insurancePremium = 200;
}
三元运算符
称为三元运算符
[队友们还记得js通过评估true或false对吗,对吧?
大多数答案都是正确的,但我想补充一点。 三元运算符