Do ‘if’ statements in JavaScript require curly braces? [duplicate]

Yes, it works, but only up to a single line just after an ‘if’ or ‘else’ statement. If multiple lines are required to be used then curly braces are necessary.

The following will work

if(foo)
   Dance with me;
else
   Sing with me;

The following will NOT work the way you want it to work.

if(foo)
   Dance with me;
   Sing with me;
else
   Sing with me;
   You don't know anything;

But if the above is corrected as in the below given way, then it works for you:

if(foo){
   Dance with me;
   Sing with me;
}else{
   Sing with me;
   You don't know anything; 
}

Leave a Comment