← ACARA Digital Technologies Years 9–10

Testing a function with console.assert

Prints the message only when the condition is false, so a test like console.assert(add(2, 3) === 5) stays silent when the function gives the expected result.

Understanding Testing a function with console.assert in ACARA Digital Technologies Years 9–10

What it does: Each console.assert line is a separate test case. Only the ones that fail will print their message, so you can see which input broke. square(3) is the actual result produced by the function, and 9 is the expected result. The === checks that they are equal. The condition must be a comparison like add(2, 3) === 5. Using = is a syntax error and does not test anything.

Common mistakes: A single = assigns a value instead of comparing, so this is not a real test (and 9 can't be assigned to a function call). The condition must use === like this: console.assert(sq(3) === 9);. The method name is misspelled. It must be console.assert with two s letters, otherwise JavaScript throws an error and the test never runs.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 710–727) and word search (Modules 710–742).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What does console.assert(add(2, 3) === 5) print when add(2, 3) returns 5?
It prints trueNothing, because the condition is true
Matches
console.assert(add(2, 3) = 5)
↓
A single = assigns a value instead of comparing. The test should use === to compare the actual result with the expected result.
Applying
Meanings
Term
console.assert(condition, message)
Meaning
Prints the message only when the condition is false, so a test like console.assert(add(2, 3) === 5) stays silent when the function gives the expected result.
Practice
function add(a, b) {
Testing
Gaps
(add(2, 3) === 5);
Check Answer
Spots
1const sq = n => n * n;
2console.assert(sq(3) = 9);

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'African Wild Dog' badge — Savannah Streak, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What does console.assert(add(2, 3) === 5) print when add(2, 3) returns 5?
A. Nothing, because the condition is true ✓
B. It prints true
console.assert stays silent when the condition is true. It only prints when the condition is false.
2. When does console.assert(condition, message) print the message?
A. Only when the condition is true
B. Only when the condition is false ✓
The message is a warning that a test failed, so it only appears when the condition is false.
3. Which line is a test that checks add(2, 3) gives the expected result of 5?
A. console.assert(add(2, 3) = 5)
B. console.assert(add(2, 3) === 5) ✓
Use === to compare the actual result with the expected result. A single = is assignment, not comparison.
4. In console.assert(double(4) === 8, "double failed"), what is the expected result?
A. 8 ✓
B. double(4)
The expected result is the value you think the function should give (8). double(4) is the actual result the function really produces.
5. Why might a programmer write several console.assert lines in a row?
A. To check the function with several different test cases ✓
B. To make the function run faster
Each console.assert is one test case. Running several in a row checks many inputs automatically.

Match the Pairs

console.assert(add(2, 3) === 5) → Tests that add(2, 3) gives the expected result 5; it stays silent if the test passes and prints an error if it fails
console.assert(isEven(4) === false, "isEven(4) failed") → Prints the message "isEven(4) failed" only if isEven(4) does not return false
Several test cases in a row check one function with different inputs, and each failing test prints its own message → console.assert(double(2) === 4, "double(2)"); console.assert(double(0) === 0, "double(0)");
Expected versus actual: the function call is the actual result and it is compared with the expected value using === → console.assert(square(3) === 9);
console.assert(add(2, 3) = 5) → A single = assigns a value instead of comparing. The test should use === to compare the actual result with the expected result.
console.assert("add(2, 3) failed", add(2, 3) === 5) → The arguments are in the wrong order. The condition must come first and the message second.

Key Term

console.assert(condition, message)
Prints the message only when the condition is false, so a test like console.assert(add(2, 3) === 5) stays silent when the function gives the expected result.

Fill in the Blanks

1. console.assert(add(2, 3) === 5);
Hint: Type the console method that runs a test and only prints something when the test fails.
2. console.assert(add(2, 3) === 5, "2+3");
Hint: Type the operator that checks the actual result is strictly equal to the expected result.
3. console.assert(add(2, 3) === 5); console.assert(add(0, 0) === 0);
Hint: Type the name of the function being tested in both test cases.
4. console.assert(double(4) === 8, "double");
Hint: Type the expected result when double is called with 4.
5. const actual = add(4, 6); const expected = 10; console.assert(actual === expected, "add");
Hint: Type the variable name that holds the value the function should produce.

Spot the Error

Question 1
A const sq = n => n * n;
B console.assert(sq(3) = 9);
C console.assert(sq(4) === 16);
Line B — A single = assigns a value instead of comparing, so this is not a real test (and 9 can't be assigned to a function call). The condition must use === like this: console.assert(sq(3) === 9);
Question 2
A console.asert(sq(5) === 25);
B console.assert(sq(2) === 4);
C function sq(n) {
D return n * n;
E }
Line A — The method name is misspelled. It must be console.assert with two s letters, otherwise JavaScript throws an error and the test never runs.
Question 3
A const got = double[4];
B const want = 8;
C console.assert(got === want);
D function double(n) {
E return n * 2;
F }
Line A — Square brackets are used for array indexes, not for calling a function. To get the actual result you must call the function with round brackets: double(4).
Question 4
A function isEven(n) {
B return n % 2 === 0;
C }
D console.assert(isEven(4);
E console.assert(!isEven(7));
Line D — The closing bracket of console.assert( is missing. The line should end with two closing brackets: console.assert(isEven(4));
Question 5
A const add = (a, b) => a + b;
B const s = add(2, 3);
C console.assert('add', s===5);
Line C — The arguments are in the wrong order. The condition must come first and the message second. Here the text 'add' is the condition, so it is always truthy and the test can never fail.

Student with a class code? Join your class →