Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript leaving an empty if statement

Tags:

javascript

I would like to know if leaving an empty if statement for certain situations as:

else if(typeof console === 'undefined'){}

Just to have the code bypass the rest of the function It is an accepted and safe way to work or there are other recommendation practices for these cases?. Thank you.

like image 691
Santiago Rebella Avatar asked Feb 19 '23 22:02

Santiago Rebella


2 Answers

It's fine and safe to leave if branches empty, the only thing I would add is a comment:

else if(typeof console === 'undefined')
{
    //explanation why nothing has to go here
}

Without seeing the rest of the code I'm unsure how you're using this to "bypass the rest of the function", there may be a better way to do this.

like image 109
martinwnet Avatar answered Feb 27 '23 10:02

martinwnet


From what information you've provided me, I can glean that the answer is "no". It will work, but it's bad style. If you would like to bypass the rest of the function, why not return; or put most of the logic in the if statement that pertains to it so that there is no bypassing at all?

like image 45
Vinay Avatar answered Feb 27 '23 11:02

Vinay