← ACARA Digital Technologies Years 7–8

Using if and else inside a function

A function can use if and else to make a decision, then return a different value from each branch. A default return at the end covers every other case.

Understanding Using if and else inside a function in ACARA Digital Technologies Years 7–8

What it does: Each branch has its own return, so the result depends on whether the condition is true or false. The final return runs only when both conditions are false, so the function never returns undefined. 60 is not at least 80, so the if branch is skipped. The default return at the end gives "B". return ends the function immediately. Any code after it in that call is skipped. 72 fails the first test but passes score >= 70, so the else if branch returns "B". The default "C" is never reached. Use return "A"; so the caller receives the grade as a value it can store or use.

Common mistakes: The closing bracket after the condition is missing. It should be if (score >= 80) {. JavaScript declares a function with the keyword function, not func. It should be function gradeFor(score) {.

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 611–623) and word search (Modules 611–634).

Take this worksheet further — free with Cloodit

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

Learning
Duo
What does gradeFor(85) return? function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
"B""A"
Matches
function gradeFor(score) { if (score >= 80) { console.log("A"); } }
↓
The branch uses console.log instead of return, so the function gives nothing back. It returns undefined.
Applying
Meanings
Term
if (score >= 80) { return "A"; }
Meaning
A function can use if and else to make a decision, then return a different value from each branch. A default return at the end covers every other case.
Practice
function gradeFor(score) {
Testing
Gaps
(score >= 50) { return "Pass"; }
Check Answer
Spots
1function gradeFor(score) {
2 if (score >= 80 {

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

The 'Hippopotamus' badge — Savannah Titan, 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 gradeFor(85) return? function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
A. "B"
B. "A" ✓
85 is at least 80, so the if branch runs and return "A" ends the function straight away.
2. What does gradeFor(60) return? function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
A. "B" ✓
B. "A"
60 is not at least 80, so the if branch is skipped. The default return at the end gives "B".
3. Which function body returns a different value from each branch?
A. if (score >= 50) { console.log("Pass"); } else { console.log("Fail"); }
B. if (score >= 50) { return "Pass"; } else { return "Fail"; } ✓
console.log only prints a message. Using return in each branch sends a value back to the code that called the function.
4. What happens after a return statement runs inside an if branch?
A. The function stops and gives back that value ✓
B. The function carries on and runs the next lines too
return ends the function immediately. Any code after it in that call is skipped.
5. Why is a default return at the end of gradeFor useful?
A. It covers every score that no if branch matched ✓
B. It makes the if statements run twice
If none of the if conditions are true, the code reaches the final return, so the function always gives back a value.

Match the Pairs

if (score >= 80) { return "A"; } → If the score is 80 or more, the function returns "A" straight away and stops running.
return "C"; → A default return at the end of the function that covers every score the earlier branches did not catch.
A function can return a different value from each branch of an if and else. → function gradeFor(score) { if (score >= 50) { return "Pass"; } else { return "Fail"; } }
Put a default return after the if statements so every other case still gets a value. → if (score >= 80) { return "A"; } if (score >= 65) { return "B"; } return "C";
function gradeFor(score) { if (score >= 80) { console.log("A"); } } → The branch uses console.log instead of return, so the function gives nothing back. It returns undefined.
function gradeFor(score) { if (score >= 80) { return "A"; } } → There is no default return. A score below 80 skips the if and the function returns undefined.

Key Term

if (score >= 80) { return "A"; }
A function can use if and else to make a decision, then return a different value from each branch. A default return at the end covers every other case.

Fill in the Blanks

1. function gradeFor(score) { if (score >= 50) { return "Pass"; } return "Fail"; }
Hint: Type the keyword that starts a decision inside the function.
2. function gradeFor(score) { if (score >= 80) { return "A"; } return "B"; }
Hint: Type the comparison operator that means 'at least'.
3. function gradeFor(score) { if (score >= 80) { return "A"; } return "C"; }
Hint: Type the keyword that sends the default value back when the if does not run.
4. function gradeFor(score) { if (score >= 50) { return "Pass"; } else { return "Fail"; } }
Hint: Type the keyword that gives the branch that runs when the if condition is false.
5. function gradeFor(score) { if (score >= 80) { return "A"; } return "C"; }
Hint: Type the parameter name that the condition checks.

Spot the Error

Question 1
A function gradeFor(score) {
B if (score >= 80 {
C return "A";
D }
E return "B";
F }
Line B — The closing bracket after the condition is missing. It should be if (score >= 80) {.
Question 2
A func gradeFor(score) {
B if (score >= 80) {
C return "A";
D }
E return "C";
F }
Line A — JavaScript declares a function with the keyword function, not func. It should be function gradeFor(score) {.
Question 3
A function isPass(mark {
B if (mark >= 50) {
C return true;
D }
E return false;
F }
Line A — The bracket after the parameter is missing. It should be function isPass(mark) {.
Question 4
A function gradeFor(s) {
B if (s >= 50) { return "P"; }
C els { return "F"; }
D }
Line C — The keyword is misspelt. It should be else, so the code runs when the if condition is false.
Question 5
A function describe(n) {
B if (n = 0) {
C return "zero";
D }
E return "not zero";
F }
Line B — A single = assigns a value instead of comparing. The condition should use ===: if (n === 0) {.

Student with a class code? Join your class →