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.
Warm-up questions
Short questions with short answers, each answered in full.
-
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
-
Fresher
How does a JavaScript switch decide which case runs, and what happens when a case has no break?
-
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
-
Fresher
What is the difference between for…of and for…in in JavaScript?
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
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
-
- How many rows does the table need when the rules read three yes-or-no facts and one amount with a limit?
- Which amounts would you put in the table?
- When would you leave the nesting as it is?
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.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
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
-
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
-
- Does a total of exactly 500 pay a fee, and how would you make that visible?
- How would you write it so that a fifth rule is one more line?
- Would a linter rule settle the argument?
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
-
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
-
- What do Number('') and parseInt('2abc', 10) return, and why do both matter here?
- Would you write case '2' or convert the value to a number?
- What should the page do with a view value that matches no case?
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
-
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
-
- What happens to deliberate fall-through and to code shared by several cases?
- How does each version handle an action name it does not know?
- How would you migrate without one big change?
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
-
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
-
- Would a step of 0.25 have the same problem?
- Why is comparing with Number.EPSILON not a general fix?
- How would you produce the labels 0.1, 0.2 and so on without long decimals?
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
-
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
-
- Does looping backwards fix it, and what does it cost?
- When would you change the array in place instead of building a new one?
- How would a test have caught the bug?
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
-
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
-
- How do you find the extra key on that page?
- Why does Object.keys(items) not show it?
- What should the library have done instead?
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
-
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
-
- Would prefixing every id with a letter fix it?
- A Map or an array of records, which would you choose, and why?
- What happens to the order when the leaderboard goes through JSON?
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