Syntax errors: missing quotes, brackets and braces

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

Syntax errors: missing quotes, brackets and braces

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line causes a SyntaxError?
A console.log("Hello);
B console.log("Hello");
2. True or false: a program with a SyntaxError will run until it reaches the broken line, then stop.
A True: the lines before the error still run
B False: JavaScript reads the code first, so nothing runs until the syntax is fixed
3. Which if statement has a missing closing brace?
A if (age > 12) { console.log("Teen");
B if (age > 12) { console.log("Teen"); }
4. What does a SyntaxError message tell you?
A Your code ran but gave the wrong answer
B Your code broke JavaScript's rules and can't be read
5. Which line has a missing closing bracket?
A console.log("Score: " + score;
B console.log("Score: " + score);

Match the Pairs

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

Left columnRight column
1console.log("Hello); Aif (age >= 13) { console.log("Teen"); }
2SyntaxError: Unexpected token '}' BA string that is never closed, because the closing quote is missing. This causes a SyntaxError.
3Every opening curly brace { needs a matching closing brace } to avoid a SyntaxError CThe closing brace } is missing at the end of the loop, so JavaScript gives a SyntaxError. Add } on a new line after console.log(i);
4Every opening quote needs a matching closing quote so the string is complete DThe closing bracket ) is missing after the condition, so JavaScript gives a SyntaxError. It should be if (score > 50) {
5if (score > 50 { console.log("Pass"); } EJavaScript found a symbol it did not expect, often because brackets or braces do not pair up.
6for (let i = 0; i < 3; i++) { console.log(i); Flet name = "Ava";

Definitions

SyntaxError

Fill in the Blanks

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

1.
// Both quotes are here, so this runs ___ pet = "Rex";
2.
function greet() { console.log("Hi"); _
3.
// A missing quote causes a ___________ console.log("Hi");
4.
// Unexpected _____ = stray symbol console.log("Hi");
5.
__ (score > 50) { console.log("Pass"); }

Spot the Error

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

Question 1
A let name = "Ava";
B console.log("Hi + name);
C console.log(name);
Question 2
A let score = 10;
B if (score > 5) {
C console.log("Pass");
D )
E console.log("Done");
Question 3
A let a = 2;
B let b = 3;
C console.log(a + b;
Question 4
A let name = "Mia";
B let age = 13;
C let city = Perth";
D console.log(city);
Question 5
A let temp = 30;
B let hot = temp > 25;
C if (hot {
D console.log("Hot");
E }

Answer Key

Syntax errors: missing quotes, brackets and braces
Section A: Quick Questions
Q1
A — console.log("Hello);
Q2
B — False: JavaScript reads the code first, so nothing runs until the syntax is fixed
Q3
A — if (age > 12) { console.log("Teen");
Q4
B — Your code broke JavaScript's rules and can't be read
Q5
A — console.log("Score: " + score;
Section B: Match the Pairs
Q1
console.log("Hello); → A string that is never closed, because the closing quote is missing. This causes a SyntaxError.
Q2
SyntaxError: Unexpected token '}' → JavaScript found a symbol it did not expect, often because brackets or braces do not pair up.
Q3
Every opening curly brace { needs a matching closing brace } to avoid a SyntaxError → if (age >= 13) { console.log("Teen"); }
Q4
Every opening quote needs a matching closing quote so the string is complete → let name = "Ava";
Q5
if (score > 50 { console.log("Pass"); } → The closing bracket ) is missing after the condition, so JavaScript gives a SyntaxError. It should be if (score > 50) {
Q6
for (let i = 0; i < 3; i++) { console.log(i); → The closing brace } is missing at the end of the loop, so JavaScript gives a SyntaxError. Add } on a new line after console.log(i);
Section C: Definitions
Q1
SyntaxError — An error shown when code breaks JavaScript's rules, like a missing quote, bracket or brace. JavaScript can't read the code, so the program won't run until you fix it.
Section D: Fill in the Blanks
Q1
// Both quotes are here, so this runs let pet = "Rex";
Q2
function greet() { console.log("Hi"); }
Q3
// A missing quote causes a SyntaxError console.log("Hi");
Q4
// Unexpected token = stray symbol console.log("Hi");
Q5
if (score > 50) { console.log("Pass"); }
Section E: Spot the Error
Q1
B — The string "Hi " is missing its closing quote, so JavaScript throws a SyntaxError (unclosed string). It should be console.log("Hi " + name);
Q2
D — The block that opened with { must be closed with a closing brace }, not a bracket ). This causes a SyntaxError. Line 4 should be }.
Q3
C — The closing bracket ) is missing, so console.log( is never closed. JavaScript gives a SyntaxError. It should be console.log(a + b);
Q4
C — The opening quote before Perth is missing, so the string is not written correctly and gives a SyntaxError. It should be let city = "Perth";
Q5
C — The condition is missing its closing bracket ) before the {. This is a SyntaxError. It should be if (hot) {