Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

JavaScript (ES2026) Module 3

Control flow interview questions

Interview questions on control flow: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.

12 questions: 4 warm-ups and 8 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher

    What is the difference between an if statement and the conditional (ternary) operator in JavaScript?

    Answered in the lesson Conditionals: if, else and the ternary operator

  2. Fresher

    How does a JavaScript switch decide which case runs, and what happens when a case has no break?

    Answered in the lesson switch statements and lookup tables

  3. Fresher

    When does a do…while loop test its condition, and when would you choose it over a while loop?

    Answered in the lesson Loops: for, while, do…while, break and continue

  4. Fresher

    What is the difference between for…of and for…in in JavaScript?

    Answered in the lesson for…of vs for…in

Questions with model answers

What each question tests, the first step of a good answer and the follow-ups to expect.

  1. Fresher to mid

    A refund function has five levels of nested if and else blocks, and a bug report says that customers who paid by gift card get the wrong message. How would you restructure the function, and how would you show that the new version gives the same answers as the old one in every other case?

    What it tests
    Turning nesting into guard clauses, listing the cases and their boundaries as a table, and proving with tests that a restructure changes nothing except the bug being fixed.
    First step
    Do not touch the code yet. List the facts the conditions read, record what the current function returns for every combination of them, and keep that table as a test. The restructured version must pass it unchanged, except for the rows the bug report says are wrong.
    Follow-ups

    From the lesson Conditionals: if, else and the ternary operator

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.

    Opening the model answers with your pass…

    Model answers open with any MySmartCoPilot pass.

    • Pro pass · 1 week₹79$2.99
    • 30 days₹179$5.99
    • 1 year₹1,699$45

    One payment; a pass never renews. The price shown is the total you pay: no fees or charges are added at checkout.

    It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  2. Fresher to mid

    In a code review you find const fee = isMember ? 0 : total > 500 ? 0 : remote ? 99 : 49; and its tests pass. Would you approve it? Explain how JavaScript groups it, what each kind of customer pays, and what you would ask the author to change.

    What it tests
    Reading how chained conditional operators group, listing every outcome, and weighing readability and boundary tests against a named function with guard clauses.
    First step
    Group it the way the language does: the third operand of each ? : may be another conditional, so it reads isMember ? 0 : (total > 500 ? 0 : (remote ? 99 : 49)). Then list the four outcomes before judging it: members, other customers above 500, remote customers and everyone else.
    Follow-ups

    From the lesson Conditionals: if, else and the ternary operator

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  3. Fresher to mid

    A page reads ?view=2 from its URL and passes the value to a switch with case 1, case 2 and case 3, but it always shows the default view. Explain why, fix it, and say what your fix does with ?view=, with ?view=2abc and with no view parameter at all.

    What it tests
    Knowing that switch compares with ===, that URL parameters arrive as strings or null, and how Number and parseInt treat empty and partly numeric text.
    First step
    Log the value and its type. URLSearchParams.get returns the string '2', or null when the parameter is missing, and a switch compares with ===, so '2' never equals 2. Then decide between switching on strings and converting to a number, and validate the value either way.
    Follow-ups

    From the lesson switch statements and lookup tables

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  4. Mid to senior

    A 30-case switch maps action names to code that updates an app's state. Bugs keep coming from a forgotten break and from a const declared in one case and used in another. Would you replace it with a Map of handler functions? Say what you gain, what you lose and how you would migrate safely.

    What it tests
    Weighing a switch against a table of handler functions: fall-through and scope bugs, unknown actions, code shared between cases, testing, and a migration in small steps.
    First step
    Look at what the cases have in common. If each one computes the new state from the old state and the action's data, each case already is a function with the same parameters, and a Map from action name to that function removes break and the shared scope by construction.
    Follow-ups

    From the lesson switch statements and lookup tables

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  5. Fresher to mid

    A chart draws tick marks with for (let x = 0; x !== 1; x += 0.1) and the page freezes. A teammate changes the test to x < 1 and now the chart shows 11 ticks instead of 10. Explain both, and write the loop so that it draws exactly the right number of ticks for any step.

    What it tests
    Why decimal steps are not exact in binary floating point, why an exact comparison never becomes true, and counting with whole numbers instead.
    First step
    Print x on each iteration. 0.1 has no exact binary value, so ten additions give 0.9999999999999999, which is not 1, and the next one gives 1.0999999999999999: x jumps over 1, so x !== 1 stays true for ever, while x < 1 is still true for the eleventh value.
    Follow-ups

    From the lesson Loops: for, while, do…while, break and continue

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Fresher to mid

    for (let i = 0; i < users.length; i++) if (users[i].inactive) users.splice(i, 1); is meant to remove every inactive user, but some stay in the array. Which ones survive, and why? Give three fixes and compare their cost on an array of 100,000 users.

    What it tests
    How removing an element shifts the indexes after it, why a forward loop skips the next element, and the cost of splice against filter and an in-place pass.
    First step
    Trace two inactive users next to each other at indexes 4 and 5. Removing index 4 moves the second one to index 4, but i++ moves the loop on to 5, so it is never checked. Every inactive user that directly follows a removed one survives.
    Follow-ups

    From the lesson Loops: for, while, do…while, break and continue

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  7. Fresher to mid

    function total(items) { let sum = 0; for (const i in items) sum += items[i].price; return sum; } passes its tests, but on one page of the app it returns NaN for the same cart. What could cause it, how would you confirm it, and how do you fix the function?

    What it tests
    Knowing that for…in over an array also visits inherited enumerable properties, how to find the extra key, and why for…of or a counting loop is the right way to walk an array.
    First step
    Ask what differs on that page: its scripts. for…in visits every enumerable key of the array, inherited ones included, so a library on that page that added an enumerable method to Array.prototype adds a key whose price is undefined, and adding undefined to a number gives NaN.
    Follow-ups

    From the lesson for…of vs for…in

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  8. Mid to senior

    A leaderboard is kept in an object keyed by player id, such as { u42: …, 1007: …, 7: … }, filled in the order the players joined. On screen, players 7 and 1007 always come first, in that order. Explain the rule behind the order, and choose a data structure that keeps the order you actually need.

    What it tests
    The property order rules for array-index keys and other string keys, where they apply, and choosing between a Map, an array of records and an explicit sort.
    First step
    Keys that are array indexes, such as '7' and '1007', are listed first in ascending numeric order, and only then the other string keys in the order they were created. Object.keys, Object.entries, for…in and JSON.stringify all follow that rule, so the join order is lost before any code sorts anything.
    Follow-ups

    From the lesson for…of vs for…in

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes

All JavaScript (ES2026) interview questions · The JavaScript (ES2026) track

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.