How will this if be interpreted?

Asked

Viewed 40 times

1

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

function Acoes(e){

   if(e == 1){...}
   if(e == 2){...}
   if(e == 3){...}
   if(e == 4 && foo == ""){...}else{...}  
}
  • to) What will happen if e be equal to 3 and foo are not empty?
  • b) The if corresponding to e == 3 shall be executed together with the else of if e == 4?
  • 1

    Those if interrupt the function? ie, there is return within those if?

  • Each if manipulates the css properties of some Ivs but the latter if depends on the variable var to be executed the same says whether the user is logged in or not, if he is not var is empty, but what if the user is logged in changing var for some value, and the if is different from 4, what happens?

1 answer

2


In answer to the questions:

to): case e be equal to 3 and the foo is not empty

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

b): e == 3 shall be executed together with the else of the if e == 4

Yes, in the case of e be of value 3. And the else will be executed regardless of the value of foo since the first condition fails?


If you want to stop more than one running every time else if. So you have a if, several else if which are run only if the if and the else if have validated earlier false, and finally a 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{...}  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.