0

Hi how do you determine when to use brackets after if statements. I thought they should be used all the time but I have seen that sometimes brackets are not used example:

timers.add(function () {
  box.style.top = y + "px";
  y += 2;
  if (y > 120) return false;

});

  • 1
    have to use them when there is more than one statement to be used within the if statement, other than that its up to you. – Patrick Evans Mar 10 '14 at 20:13
  • @PatrickEvans you should post that as an answer instead of a comment, so you can get points for being right! :) I would have given you an upvote.... – BrettFromLA Mar 10 '14 at 20:14
  • @BrettFromLA, I believe this is a duplicate (looking now). – Patrick Evans Mar 10 '14 at 20:15
  • @PatrickEvans Most likely. – BrettFromLA Mar 10 '14 at 20:17
  • **Always** (my personal view); however, this is opinion-based - except where it obviously changes the semantics and is a non-question - so no answer and closing .. – user2864740 Mar 10 '14 at 20:20
  • Pretty much anytime you have something that has a body (e.g. if/else) if it does not define a block using braces it is implied that the next line is the body of the if. There are many reasons why this approach is not usually advisable, see this [question](http://stackoverflow.com/questions/4797286/are-curly-braces-necessary-in-one-line-statements-in-javascript) for a better explanation of when to use them. – user3334690 Mar 10 '14 at 20:20

2 Answers2

2

Really, you should always add them for readability. You can omit them when the following statement is the only one you want to include in the if block. If you have two statements after the if, the second one will always execute after the if block is done when no brackets are present. In the code below example 1 will set a = 5. In example 2 only alert will fire, but is hard to read. Example 3 is the same as 2, but is easy to read.

// Example 1
    if (5 == 5) a = 5;

// Example 2
    if (5 == 10) a = 5; alert("hello");

// Example 3
    if (5 == 10) {
       a = 5; 
    }
    alert("hello");
CrazyDart
  • 3,803
  • 2
  • 23
  • 29
  • +1 for the first sentence, in particular. In an example like this - http://pastebin.com/n767wB1g - you can very easily fool yourself with the indentation. If you include the brackets, it's always clear! – CompuChip Mar 10 '14 at 20:20
  • I know people will think writing this line would be crazy `if (5 == 10) a = 5; alert("hello");`, but I have seen developers modify one line if statements to look just like that. – David Sherret Mar 10 '14 at 20:21
  • You might also note things like (bool) ? a : b which I am pretty sure is a thing in javascript and might be relevant to the asker since they are mostly asking about valid syntax rather than specific examples... – user3334690 Mar 10 '14 at 20:23
  • Thank you all for your responses, It is very clear now and I don't have to be guessing. – user3403387 Mar 10 '14 at 20:26
  • @user3334690 yes, the ternary operator also seems to confuse people. I don't like to use them... but sometimes they are more readable. – CrazyDart Mar 10 '14 at 20:27
0

After an if statement, one statement is executed under that condition. Something like 'return false;' is one statement - you see that because after each statement, there's a semicolon (';').
If you want to apply the condition for several statements, you can merge them by putting curly braces around them ('{}').
So for one statement, the braces are not necessary, but can be useful for better readability of the code.

Note that the 'else if' statement is not actually it's own thing, but rather an 'else' triggering an 'if'. You could also put an 'else' and an 'if' within braces, but if you have very long 'else if' chains, the braces get really messy (sure I can tell :D).
So if you have a 'while' or 'do while' or 'for' or anything else that already groups things together, just put i.e. 'if (condition) while (condition){}'. This works for all loops and conditional statements: If, else, for, while, do while. Switch-case and every other thing only at the end of such "chains".

'Else if' example:

else if (a<b) {
alert.("Jay!");
}

is equal to

else {
if (a<b){
alert.("Jay!");
}
}

Another example:

while(a<b) for(xyz){
alert.("Jay!");
}

Couldn't resist it :D (this is actually valid code: )

if(a<b) while(c>=d) for(xyz) switch(e){

case 1: alert('Hey there!');   break;
case 2: alert('Coding is fun');   break;
case 3: alert('Just wait for the goofy little projects xD');   break;

}
Sam
  • 251
  • 1
  • 19