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
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744298048a4567358.html
评论列表(0条)