If statements curly braces

I am working on creating a calendar using JavaScript.

I am getting different results between format 1 and format 2. Format 1 works great. Format 2 is giving problems. Aren’t format 1 and 2 the same?

Format 1
if (weekDay === 0) htmlCode += “”;
htmlCode += “” + i + “”;
if (weekDay === 6) htmlCode += “”;

Format 2
if (weekDay === 0) {
htmlCode += “”;
htmlCode += “” + i + “”;
}
if (weekDay === 6) {
htmlCode += “”;
}

1 Like

Dear @text6210452097
the difference between your Format 1 and Format 2 is that everything you do in Format 2 is bound to the condition due to the curly braces. If you don’t use them as in Format 1, only the first line of code, here: htmlCode += "";is bound to the if statement. The other line htmlCode += “” + i + “”; is implemented anyway, independent of any condition.

1 Like