Why do we start a new line after "{" during the if statement task?

// in the below example the first if statement is given my codecademy. you see how there is a new line after if (true {. Why is that the case? Why is it not all on one line like in the second example I wrote?. Thanks

[/codebyte]
if (true) {
console.log(‘This message will print!’);
}
// Prints: This message will print!

let sale = true;
if (sale) {console.log(‘Time to buy!’)};

Hi there, and welcome to the forums!

There’s no syntactical reason for this, it doesn’t change how it functions or make the code run faster or anything like that. The reason we do this is for ease of readability. In this particular case the single line if statement isn’t hard to read at all, and could be argued that it’s actually cleaner looking. However often we’ll have larger if statements, and sometimes these will be nested too.
In most text editors and IDE’s they will format the code with indents when you take a new line like that, and it just makes it a lot easier to read from a human perspective. After all, we want other developers to be able to easily read and understand our code! But as I mentioned, in this case I think it’s fair to say both look good and readable, and often with small ones like that I will choose the single line, it’s really about preference!

2 Likes