Curly braces and indentation

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Curly braces and indentation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which if statement uses curly braces correctly?
A if (score > 10) { console.log("Win"); }
B if (score > 10) { console.log("Win");
2. Where does the opening brace usually go in a for loop?
A On the same line as the for (...) part
B On a line far below the loop, after the body
3. Where does the closing brace } go?
A On its own line, lined up with the start of the if, loop or function
B At the end of the last statement inside the block, on the same line
4. Which function is indented correctly?
A function greet() { console.log("Hi"); }
B function greet() { console.log("Hi"); }
5. True or false: Curly braces { } group the statements that belong to an if, loop or function.
A False
B True

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1if (score > 50) { console.log("Pass"); } AThe opening brace { sits on the same line as the if. The statement inside the braces is indented, and the closing brace } sits on its own line.
2for (let i = 0; i < 3; i++) { console.log(i); } Bfunction greet() { console.log("Hi"); }
3Curly braces group all the statements that belong to a function, and the body is indented inside them. CThe braces group the statements that repeat in the loop. Only the indented code inside { } runs each time.
4Several statements can share one block. Every line inside the braces is indented the same amount. DThe opening brace is not on the same line as the if, and the statement inside is not indented, so the block is hard to read.
5if (age >= 13) { console.log("Teen"); EThe closing brace } is missing. Every opening { needs a matching } on its own line after the block.
6if (age >= 13) { console.log("Teen"); } Fwhile (lives > 0) { lives--; console.log(lives); }

Definitions

{ }

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
if (score >= 50) _ console.log("Pass"); }
2.
for (let i = 0; i < 3; i++) { console.log(i); _
3.
function sayHi() _ console.log("Hi!"); }
4.
function double(n) { ______ n * 2; }
5.
while (lives > 0) { lives--; _

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A function pass(score) {
B return score > 50;
C )
Question 2
A function add(a, b)
B return a + b;
C }
Question 3
A let ok = true;
B if (ok) [
C console.log("Go");
D }
Question 4
A let n = 3;
B while (n > 0) (
C console.log(n);
D n--;
E }
Question 5
A function isEven(num) {
B if (num % 2 === 0) {
C return true;
D )
E return false;
F }

Answer Key

Curly braces and indentation
Section A: Quick Questions
Q1
A — if (score > 10) { console.log("Win"); }
Q2
A — On the same line as the for (...) part
Q3
A — On its own line, lined up with the start of the if, loop or function
Q4
B — function greet() { console.log("Hi"); }
Q5
B — True
Section B: Match the Pairs
Q1
if (score > 50) { console.log("Pass"); } → The opening brace { sits on the same line as the if. The statement inside the braces is indented, and the closing brace } sits on its own line.
Q2
for (let i = 0; i < 3; i++) { console.log(i); } → The braces group the statements that repeat in the loop. Only the indented code inside { } runs each time.
Q3
Curly braces group all the statements that belong to a function, and the body is indented inside them. → function greet() { console.log("Hi"); }
Q4
Several statements can share one block. Every line inside the braces is indented the same amount. → while (lives > 0) { lives--; console.log(lives); }
Q5
if (age >= 13) { console.log("Teen"); → The closing brace } is missing. Every opening { needs a matching } on its own line after the block.
Q6
if (age >= 13) { console.log("Teen"); } → The opening brace is not on the same line as the if, and the statement inside is not indented, so the block is hard to read.
Section C: Definitions
Q1
{ } — Curly braces group the statements that belong to an if, loop or function. The code inside the braces is indented so you can see which block it belongs to.
Section D: Fill in the Blanks
Q1
if (score >= 50) { console.log("Pass"); }
Q2
for (let i = 0; i < 3; i++) { console.log(i); }
Q3
function sayHi() { console.log("Hi!"); }
Q4
function double(n) { return n * 2; }
Q5
while (lives > 0) { lives--; }
Section E: Spot the Error
Q1
C — The function block is closed with a round bracket. It must be closed with a curly brace } on its own line.
Q2
A — The opening curly brace { is missing. It should go at the end of the first line: function add(a, b) {
Q3
B — A square bracket [ was used to open the block. An if block must open with a curly brace { on the same line as the if.
Q4
B — A round bracket ( was used to open the loop body. A block must open with a curly brace {.
Q5
D — The if block is closed with a round bracket. It must be closed with a curly brace }, on its own line, lined up with the if.