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.

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

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:

B

Conditions 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 prices

switch 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.

  1. 'At least 50' is total >= 50.
  2. 'or' means either condition is enough: ||.
  3. Member is a Boolean, so it can be tested directly.

Answer: `if (total >= 50 || member) { delivery = 0; }`

Key idea: if / else if / else checks conditions in order and runs only the first true branch, so order the boundaries carefully. && needs both, || needs either, ! negates. switch compares one value with cases; remember break.

Check yourself

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.