JS - If multiple conditions can be true at the same time, which one runs?

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-11 12:28:55

问题


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

  1. if (friendsAtYourParty === 0) { // 8 == 0 returns false so it skips this block

  2. console.log("Cool, now I have a lot of nachos to myself.");

  3. } else if (friendsAtYourParty < 4) { // 8 < 4 returns false so it skips this block

  4. console.log("Perfect amount to play some Mario Kart.");

  5. } else { // there is no condition in else , so if all conditions fail in above blocks it will execute this

  6. console.log("Wooooo turn on the dance music!");

  7. }

Lets consider another scenario where friendsAtYourParty is 0

  1. 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

  2. console.log("Cool, now I have a lot of nachos to myself.");

  3. } else if (friendsAtYourParty < 4) { // 0 < 4 returns true but its ignored

  4. console.log("Perfect amount to play some Mario Kart.");

  5. } else { // this is also ignored

  6. console.log("Wooooo turn on the dance music!");

  7. }

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!