1

I am having trouble understanding the usage of brackets in "for" loops and "if" statements in Javascript. I have seen syntax in Javascript where there is brackets and where there isn't. I was told that generally one should use the brackets. Can someone clearly explain when we should use brackets for "for" and "if" loops?

function range(upto) {
  var result = [];
  for (var i = 0; i <= upto; i++)
    result[i] = i;
  return result;
}
console.log(range(15));

The result of this would be

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

This is vs. the brackets:

function range(upto) {
  var result = [];
  for (var i = 0; i <= upto; i++) {
    result[i] = i;
  };
    return result;

}
console.log(range(15));
2
  • 1
    The question isn't clear anymore because they produce the same result. What is the question? Commented Jul 2, 2013 at 18:41
  • The question is when should brackets be used for "for loops" and "if statements". It has been answered well below. Commented Jul 2, 2013 at 18:53

3 Answers 3

2

You can omit the brackets when the for loop applies to a unique statement.

You must use it if you want it to apply to more statements. To be more precise, the for loop always apply to a statement but the brackets build a block which is a statement.

Adding the brackets, when there is only one statement, can't break the code and often makes it more readable. In fact most coders won't go to the next line in an opening for loop without brackets. I personally would replace

for (var i = 0; i <= upto; i++)
    result[i] = i;

with

for (var i = 0; i <= upto; i++) result[i] = i;

or

for (var i = 0; i <= upto; i++) {
    result[i] = i;
}
Sign up to request clarification or add additional context in comments.

3 Comments

both snippets look identical to me.
@thg435 You're right. But the question was edited, I think I did read something different, and that there even was the output of the second code with a single element array...
yea i edited it earlier, silly mistake. sorry for the confusion
1

Without brackets only the next statement is affected whereas with brackets everything inside the brackets is affected.

To add brackets to the loop and have it work exactly the same, just add them around the next statement:

function range(upto) {
    var result = [];
    for (var i = 0; i <= upto; i++) {
        result[i] = i;
    }
    return result;
}

Comments

0

Bracket allow you to add more statement into one block. if I modify bit to show result

function range(upto) {
  var result = [];
  for (var i = 0; i <= upto; i++) {
    result[i] = i;
    result[i] = result[i]*2
  }
    return result;

}
console.log(range(15));

Result will be

[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30]

however, without bracket,

function range(upto) {
      var result = [];
      for (var i = 0; i <= upto; i++) 
        result[i] = i;
        result[i] = result[i]*2

        return result;

    }
    console.log(range(15));

result will be like this

[1,2,3,4,5,6,7,8,9,10,11,12,13,14,30]

*being rookie programmer, I think it probably will fail due to undeclared variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.