All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript
Selection in JavaScript: if, else if, else and switch
Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript
Selection chooses which code runs depending on a condition (spec 9.3.2 d). It is used for validation, grading and game logic.
if, else if, else
let mark = 64;
let grade;
if (mark >= 70) {
grade = "A";
} else if (mark >= 60) {
grade = "B";
} else if (mark >= 50) {
grade = "C";
} else {
grade = "U";
}
console.log(grade);Output:
BConditions are checked in order and only the first true branch runs. That is why the highest boundary is tested first: if mark >= 50 came first, a mark of 64 would get C.
Combining conditions
let age = 17;
let member = true;
if (age >= 16 && age <= 99) {
console.log("Age accepted");
}
if (age < 18 || !member) {
console.log("Adult supervision needed");
}Output:
Age accepted
Adult supervision needed&& is true only if both sides are true; || is true if at least one side is true; ! reverses a Boolean. A range check needs &&: age >= 16 && age <= 99. Using || here would accept every age.
switch
let day = "Sat";
switch (day) {
case "Sat":
case "Sun":
console.log("Weekend prices");
break;
case "Mon":
console.log("Closed");
break;
default:
console.log("Weekday prices");
}Output:
Weekend pricesswitch compares one value against several cases with ===. break stops it running on into the next case; cases without code share the next block. default runs if nothing matches. Two separate if statements can also be used instead of else if; Pearson's 2024 mark scheme accepted either.
Worked example
A shop gives free delivery when the basket total is at least 50 or the customer is a member. Write the condition.
- 'At least 50' is total >= 50.
- 'or' means either condition is enough: ||.
- Member is a Boolean, so it can be tested directly.
Answer: `if (total >= 50 || member) { delivery = 0; }`
Check yourself
- What is displayed when this code runs?
```js
let mark = 40;
let grade = "Fail";
if (mark > 40) {
grade = "Pass";
}
console.log(grade);
```
Show answer
Fail. 40 > 40 is false, so grade stays Fail. If 40 is meant to pass, the condition should be mark >= 40.
- What is displayed when this code runs?
```js
let t = 25;
if (t > 30) {
console.log("Hot");
} else if (t > 20) {
console.log("Warm");
} else if (t > 10) {
console.log("Mild");
} else {
console.log("Cold");
}
```
Show answer
Warm. 25 > 30 is false; 25 > 20 is true, so Warm runs and the rest are skipped.
- What is displayed when this code runs?
```js
let age = 15;
let ok = age >= 16 && age <= 18;
console.log(ok);
```
- false
- true
- 15
- undefined
Show answer
false. 15 >= 16 is false, so the whole && is false: false.
- What is displayed when this code runs? (Output lines are separated by / in the options.)
```js
let size = "M";
switch (size) {
case "S":
console.log("Small");
case "M":
console.log("Medium");
case "L":
console.log("Large");
break;
default:
console.log("Unknown");
}
```
- Medium
- Medium / Large / Unknown
- Small / Medium / Large
- Medium / Large
Show answer
Medium / Large. There is no break after case "M", so it runs on into case "L" until the break: Medium, then Large.
- This code should display Valid only for ages from 18 to 65 inclusive. Which condition completes it?
```js
let ages = [17, 18, 65, 66];
for (let age of ages) {
if (__________) {
console.log(age + " Valid");
}
}
```
- `age > 18 && age < 65`
- `age >= 18 && age <= 65`
- `age >= 18 || age <= 65`
- `age > 17 || age < 66`
Show answer
`age >= 18 && age <= 65`. A range check with inclusive boundaries needs >= and <= joined by &&. With || every age passes; with > and < the boundaries fail.
- Why must a grading program test mark >= 70 before mark >= 50?
- JavaScript checks conditions from the bottom up
- else if only works with descending numbers
- The second test would cause an error
- Only the first true condition runs, so a mark of 75 would otherwise get the lower grade
Show answer
Only the first true condition runs, so a mark of 75 would otherwise get the lower grade. 75 >= 50 is true, so if that test came first, 75 would be given the lower grade.
- What is displayed when this code runs?
```js
let loggedIn = false;
if (!loggedIn) {
console.log("Please log in");
} else {
console.log("Welcome");
}
```
Show answer
Please log in. !false is true, so Please log in is displayed.
Sources
- Pearson Edexcel International Advanced Subsidiary/Advanced Level in Information Technology specification (first teaching 2018), Unit 2 Topics 7 to 11
- Pearson qualifications: Edexcel International Advanced Level Information Technology (2018)
- Pearson WIT12/01 mark scheme, June 2024 series
- Pearson WIT12/01 question paper, October 2021 series
Last updated 8 November 2026. Spotted a mistake? Tell us.