← ACARA Digital Technologies Years 7–8

Reading user input with prompt()

Shows a message box, waits for the user to type, then returns what they typed as a string. Store it in a variable. Cancel returns null.

Understanding Reading user input with prompt() in ACARA Digital Technologies Years 7–8

What it does: Pressing Cancel makes prompt() return null, so comparing the stored answer to null detects a cancel. Number() wraps the prompt() result and turns the typed text into a number that can be used in arithmetic. Input from prompt() is always a string, even when the user types digits. prompt() pauses the program with a box, while console.log() just prints to the console. Cancel gives back null. Clicking OK with nothing typed gives an empty string "" instead. The + operator joins strings, so "5" + "3" becomes "53". Use Number() to convert first. You must call prompt with brackets and a message. Without them you only refer to the function and never ask anything.

Common mistakes: The function name is misspelled. It must be prompt, with the letter p before t, otherwise JavaScript throws a ReferenceError. The variable is called next, but this line uses nex, which was never declared. It should be next.

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 562–580) and word search (Modules 522–580).

Take this worksheet further — free with Cloodit

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

Learning
Duo
Which line stores what the user types in a variable called name?
let name = prompt("Your name?");prompt("Your name?") = name;
Matches
prompt("Your name?"); console.log("Hi " + name);
↓
The answer was never stored in a variable, so name does not exist. Use let name = prompt("Your name?");
Applying
Meanings
Term
prompt("question")
Meaning
Shows a message box, waits for the user to type, then returns what they typed as a string. Store it in a variable. Cancel returns null.
Practice
let age = prompt("How old are you?");
Testing
Gaps
let name = ("What is your name?");
Check Answer
Spots
1// ask for a name
2let name = promt("Name?");

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

The 'Barreleye Fish' badge — Clear Vision, 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. Which line stores what the user types in a variable called name?
A. let name = prompt("Your name?"); ✓
B. prompt("Your name?") = name;
prompt() returns the typed text, so you put it on the right side of = to save it in a variable.
2. A user types 14 into prompt("Age?"). What data type does prompt() return?
A. A number: 14
B. A string: "14" ✓
Input from prompt() is always a string, even when the user types digits.
3. What does prompt("Favourite subject?") do when it runs?
A. Shows a message box, waits for typing and returns the text ✓
B. Prints the message in the console and carries on straight away
prompt() pauses the program with a box, while console.log() just prints to the console.
4. What does prompt() return if the user clicks Cancel?
A. An empty number 0
B. null ✓
Cancel gives back null. Clicking OK with nothing typed gives an empty string "" instead.
5. let a = prompt("First?"); with the user typing 5 and then 3 in a second prompt stored in b. What does a + b give?
A. "53" because both are strings ✓
B. 8 because they are numbers
The + operator joins strings, so "5" + "3" becomes "53". Use Number() to convert first.

Match the Pairs

let name = prompt("Your name?"); → Shows a message box, waits for the user to type, and stores what they typed in the variable name
let age = prompt("Your age?"); console.log(typeof age); → Prints string, because prompt() always returns text, even when the user types digits
prompt() returns null when the user presses Cancel, so you can check for it → if (answer === null) { console.log("Cancelled"); }
Input is a string, so convert it with Number() before doing maths → let n = Number(prompt("Pick a number"));
prompt("Your name?"); console.log("Hi " + name); → The answer was never stored in a variable, so name does not exist. Use let name = prompt("Your name?");
let a = prompt("First number?"); let total = a + 5; → a is a string, so a + 5 joins text instead of adding. If the user types 3 the result is "35". Use Number(prompt(...)).

Key Term

prompt("question")
Shows a message box, waits for the user to type, then returns what they typed as a string. Store it in a variable. Cancel returns null.

Fill in the Blanks

1. let name = prompt("What is your name?");
Hint: Type the function that shows a message box and waits for the user to type.
2. let name = prompt("Your name?"); console.log("Hi " + name);
Hint: Type the variable that stores what the user typed.
3. let age = prompt("Age?"); console.log(typeof age);
Hint: Type the operator that shows the data type of the stored answer (a string).
4. let name = prompt("Your name?"); if (name === null) { console.log("Cancelled"); }
Hint: Type the special value prompt() returns when the user presses Cancel.
5. let num = Number(prompt("Enter a number:"));
Hint: Type the function that turns the typed string into a number.

Spot the Error

Question 1
A // ask for a name
B let name = promt("Name?");
C console.log("Hi " + name);
Line B — The function name is misspelled. It must be prompt, with the letter p before t, otherwise JavaScript throws a ReferenceError.
Question 2
A let age = prompt("Age?");
B let next = Number(age) + 1;
C console.log("Next: " + nex);
Line C — The variable is called next, but this line uses nex, which was never declared. It should be next.
Question 3
A let n = prompt("Number?");
B let total = Number(n) + 5;
C console.log("Total: " + total;
Line C — The closing bracket of console.log( is missing. It should end with total);
Question 4
A let ans = prompt("Number?");
B let n = Number(ans);
C let total = n + 5;
D console.log("Sum:", ans + 5);
Line D — prompt() always returns a string, so ans + 5 joins the text, for example "35" instead of 8. Use the converted number: total (or n + 5).
Question 5
A let city = prompt(City?);
B console.log("Hello " + city);
C console.log(city.length);
Line A — The question inside prompt() is text, so it needs quotation marks: prompt("City?").

Student with a class code? Join your class →