admin管理员组文章数量:1401143
I am trying to understand ternary operators. However, I am now stumped! I want to use && in the statement.
This is the code that I am trying to create using the ternary operator:
if (eatsPlants === true && eatsAnimals === true) {
"omnivor";
} else {
"undefined";
}
This is what I have tried:
(eatsPlants && eatsAnimals) ? "omnivore" : "undefind";
Here is the whole code that I have so far:
var eatsPlants = true;
var eatsAnimals = true;
var category =
eatsPlants ? "herbivore" : "carnivor";
eatsAnimals ? "carnivor" : "herbivor";
(eatsPlants && eatsAnimals) ? "omnivore" : "undefined";
console.log(category);
I am trying to understand ternary operators. However, I am now stumped! I want to use && in the statement.
This is the code that I am trying to create using the ternary operator:
if (eatsPlants === true && eatsAnimals === true) {
"omnivor";
} else {
"undefined";
}
This is what I have tried:
(eatsPlants && eatsAnimals) ? "omnivore" : "undefind";
Here is the whole code that I have so far:
var eatsPlants = true;
var eatsAnimals = true;
var category =
eatsPlants ? "herbivore" : "carnivor";
eatsAnimals ? "carnivor" : "herbivor";
(eatsPlants && eatsAnimals) ? "omnivore" : "undefined";
console.log(category);
Share
Improve this question
asked Jun 26, 2019 at 6:04
DigitalM0nkeyDigitalM0nkey
1972 gold badges3 silver badges8 bronze badges
2
-
2
"omnivor";
doesn't do anything. Your originalif
statement has no effect. It's unclear what you're trying to achieve. – melpomene Commented Jun 26, 2019 at 6:06 -
3
The last two lines before your
console.log
doesn't do anything. You are not setting any new value to any variable. – Gustav G Commented Jun 26, 2019 at 6:07
2 Answers
Reset to default 3Your problem has nothing to do with &&
, but with ?:
syntax, and with syntax of JavaScript statements overall.
var category =
eatsPlants ? "herbivore" : "carnivor";
eatsAnimals ? "carnivor" : "herbivor";
(eatsPlants && eatsAnimals) ? "omnivore" : "undefined";
is three separate statements: one assignment that depends on eatsPlants
, and two evaluations whose results will be discarded. This will do what you wanted:
var category =
(eatsPlants && eatsAnimals) ? "omnivore" :
eatsPlants ? "herbivore" :
eatsAnimals ? "carnivore" : "undefined";
The reason this works is the precedence rules that say that ?:
binds from the right; i.e. the previous code is equivalent to
var category =
(eatsPlants && eatsAnimals) ? "omnivore" :
(eatsPlants ? "herbivore" :
(eatsAnimals ? "carnivore" : "undefined"));
When you are using tradition if-else, a statement is what you put inside the if-else block. Simply putting an expression(which then resolves to a value) does nothing but giving the value. So there is not value assignment happens.
For ternary operators, it is an expression that resolves to a value, which you can assign to some variable
let category ;
if (eatsPlants === true && eatsAnimals === true) {
category = "omnivor"; // statement
} else {
category = "undefined";
}
let category2 = (eatsPlants === true && eatsAnimals === true)? "omnivor": "undefined"; // expression
本文标签: javascriptHow do I use ampamp in a ternary operator statementStack Overflow
版权声明:本文标题:javascript - How do I use && in a ternary operator statement? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744298047a2599439.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论