How will this if be interpreted?

1

I have a function that performs different actions on the page according to the callback passed to it, I am using if to check which callback was called in function('callback') but now I had to do an update on it and now it is like this :

function Acoes(e){

   if(e == 1){...}
   if(e == 2){...}
   if(e == 3){...}
   if(e == 4 && foo == ""){...}else{...}  
}
  • a) What will happen if e is equal to 3 and foo is not empty?
  • b) The if corresponding to e == 3 will be executed along else of if e == 4 ?
asked by anonymous 22.12.2016 / 22:46

1 answer

2

In response to questions:

  

a): If e is equal to 3 and foo is not empty

if(e == 3){...} gives true and executes, if(e == 4 && foo == "") gives false and will execute else .

  

b): e == 3 will be executed along with else of if e == 4

Yes, if e has the value 3 . And will else run regardless of the value of foo since the first condition fails?

If you want to prevent more than one run at a time, you can use else if . So you have if , several else if that are run only if if and else if have validated false , and finally else if all have validated false .

if(e == 1){...}
else if(e == 2){...}
else if(e == 3){...}
else if(e == 4){...}
else if (foo == "")
else{...}  
    
22.12.2016 / 23:00