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 original if 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
Add a ment  | 

2 Answers 2

Reset to default 3

Your 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