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));