问题
If multiple if-statements can be true at the same time, how does JS determine which one to run? In the example below, I know "Cool, now I have a lot of nachos to myself." will get returned, but I just wonder how that is determined? What if instead of friendsAtYourParty === 0
it would be friendsAtYourParty < 3
("Cool, now I have a lot of nachos to myself." would get returned again, but why?). I was thinking maybe the one that is more specific/closer to the value "wins"?
if (friendsAtYourParty === 0) {
console.log("Cool, now I have a lot of nachos to myself.");
} else if (friendsAtYourParty < 4) {
console.log("Perfect amount to play some Mario Kart.");
} else {
console.log("Wooooo turn on the dance music!");
}
回答1:
When you have an if/else
chain only the first if
condition that matches would be executed. After all, that's how if/else
works:
if (condition) {
//only executed when condition is true
} else {
//only executed when condition is false
}
The else
part will NOT execute when the condition is true
. Nothing changes when you have more than one if/else
:
if (condition1) {
//only executed when condition1 is true
} else if (condition2) {
//only executed when condition2 is true and condition1 is false
} else {
//only executed when both condition1 and condition2 are false
}
An if/else
chain describes the following logic flow, hence why it will always be evaluated predictably:
回答2:
Basically first true condition runs. It happens because JS code gets run and executed line by line. When JS engine sees if statement and the condition is false it continues to read the code. When it sees the condition that is true it executes the statement declared after the condition and then removes if statement from a callstack. In other words: It is not going to read other conditions after it saw true. It will ignore them
回答3:
Lets consider friendsAtYourParty value is 8
if (friendsAtYourParty === 0) { // 8 == 0 returns false so it skips this block
console.log("Cool, now I have a lot of nachos to myself.");
} else if (friendsAtYourParty < 4) { // 8 < 4 returns false so it skips this block
console.log("Perfect amount to play some Mario Kart.");
} else { // there is no condition in else , so if all conditions fail in above blocks it will execute this
console.log("Wooooo turn on the dance music!");
}
Lets consider another scenario where friendsAtYourParty is 0
if (friendsAtYourParty === 0) { // 0 == 0 returns true so it runs this block and gets out of if else chain and ignores all blocks in if else chain
console.log("Cool, now I have a lot of nachos to myself.");
} else if (friendsAtYourParty < 4) { // 0 < 4 returns true but its ignored
console.log("Perfect amount to play some Mario Kart.");
} else { // this is also ignored
console.log("Wooooo turn on the dance music!");
}
Press Ctrl+shift+i to open dev tools , run the following snippet and press f19 to go step by step to understand the flow.
debugger
var friendsAtYourParty=8;
if (friendsAtYourParty === 0) {
console.log("Cool, now I have a lot of nachos to myself.");
} else if (friendsAtYourParty < 4) {
console.log("Perfect amount to play some Mario Kart.");
} else {
console.log("Wooooo turn on the dance music!");
}
friendsAtYourParty=0;
if (friendsAtYourParty === 0) {
console.log("Cool, now I have a lot of nachos to myself.");
} else if (friendsAtYourParty < 4) {
console.log("Perfect amount to play some Mario Kart.");
} else {
console.log("Wooooo turn on the dance music!");
}
来源:https://stackoverflow.com/questions/60839245/js-if-multiple-conditions-can-be-true-at-the-same-time-which-one-runs