Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Reading Blocks by Their Braces

Reading Blocks by Their Braces

In this lesson, you found out why the release note’s JavaScript port printed the triage call on a day with no open bugs, and how to put that line inside the if.

  • Python reads a block from its indentation, but JavaScript doesn’t read indentation at all. An if without braces controls exactly one statement, so the next line runs every time, however it’s indented.
  • Curly braces make the block: the opening brace after the condition starts it, and the closing brace ends it. A braced block counts as one statement, so the if controls all of it.
  • Always write the braces, even around one line, so a line someone adds under an if lands inside the block. A formatter then lines each indent up with the braces you wrote.

Here’s the port as the teammate left it, on a day with no open bugs. The second console.log() line was added today, indented like the one above:

Language: JavaScript
const BUGS_OPENED = 9;
const BUGS_CLOSED = 9;
const openBugs = BUGS_OPENED - BUGS_CLOSED;
console.log(`Open bugs: ${openBugs}`);

if (openBugs > 0)
  console.log("Please check the bug tracker.");
  console.log("Triage call at 9:30 tomorrow.");

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:02 Here’s the release note’s bug check on a day with no open bugs. Python skipped both lines under its if. The JavaScript version printed the triage call anyway.

00:14 Until today, that if had one line. Today, a teammate added the triage line, indented like the one above. In this lesson, you’ll find out why that line always runs, and how to make the if control it.

00:32 Python reads its block from the indentation, but JavaScript doesn’t read indentation at all. An if without braces controls exactly one statement: one complete instruction, here the bug tracker line.

00:47 The triage line is the next statement. It comes after the if, not inside it. So it runs every time, open bugs or not. The two spaces in front of it are there only for readers.

01:03 Wrap both lines in curly braces. The opening brace starts the block, and the closing brace ends it. A braced block counts as one statement, so the if now controls both lines.

01:17 Run it again: open bugs zero and nothing else, the same as Python’s. Watch out: Python’s header typed into JavaScript, with no parentheses and a colon at the end, stops the file with a SyntaxError before anything runs.

01:36 Now suppose an edit loses the triage line’s indent, so it starts at the margin. With no open bugs, does the triage call print? Your turn: pause the video and predict.

01:54 No. Open bugs zero, and nothing else: the triage line is still inside the braces. The braces decide, whatever the indentation says.

02:07 So here’s the rule. Always write the braces, even around one line, so a line someone adds under an if lands inside the block. A formatter then puts each indent back in line with the braces you wrote.

02:24 Next up is the text lesson Writing Empty Blocks and Comments, where the script’s else branch holds only pass and a reminder to do later.

Become a Member to join the conversation.