JavaScript (ES2026) Module 9
Errors and debugging interview questions
Interview questions on errors and debugging: 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 happens when a finally block contains a return statement?
-
Fresher
What is the difference between TypeError and RangeError, and when should your own code throw each?
Answered in the lesson Built-in error types and engine messages
-
Fresher
What does the cause option of the Error constructor do, and why pass it when you rethrow?
Answered in the lesson Custom errors, error cause and Error.isError
-
Fresher
What does the debugger statement do, and when does it pause the program?
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 nightly job imports a CSV file of orders and wraps each row in try … catch, logging "skipped bad row" and moving on. After a refactor, about 30 % of the rows are skipped every night, and nobody notices for a week. What went wrong, and how would you restructure the error handling?
- What it tests
- Telling expected input errors from bugs, catching narrowly and rethrowing the rest, and making skipped work visible with counts and limits while one bad row still cannot stop the whole import.
- First step
- Look at what the catch block actually caught before changing anything. Log the name and message of a few skipped rows, or run one of them without the try: a TypeError or ReferenceError from the refactored code means the catch has been hiding a bug, not bad data.
- Follow-ups
-
- Which errors should still skip a row, and how does the code tell them apart from bugs?
- How would you make a jump from 1 % to 30 % skipped rows stop the job or raise an alert?
- Where should the try … catch sit, around each row, around the whole file, or both?
From the lesson try, catch, finally and throw
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 4 minutes
-
Mid to senior
A function writes a report to a temporary file and deletes the file in finally. In production the logs show only "EPERM: operation not permitted, unlink" and never the original failure. Explain why the first error is missing, and change the function so that callers see what really went wrong.
- What it tests
- How a throw inside finally replaces a pending error, choosing which error a caller receives, and keeping the second failure visible, including with SuppressedError and DisposableStack where the runtime has them.
- First step
- Trace the two failures in order. Writing the report threw, that error was on its way to the caller, finally ran, and the unlink call threw its own error, which replaced the first one. So the EPERM is real but secondary; the fix starts by catching the cleanup's error inside finally.
- Follow-ups
-
- When is it right to let the cleanup error reach the caller?
- How would you give the caller both errors instead of logging one of them?
- What would a using declaration with DisposableStack change in this function?
From the lesson try, catch, finally and throw
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
You maintain a payments library that 40 services use. A teammate wants to change the TypeError it throws for an amount given as text into a RangeError, to make the library consistent. What do you weigh before agreeing, and how would you ship the change?
- What it tests
- Treating error classes and codes as part of a public API, judging who breaks when a class changes, and shipping with semantic versioning, a deprecation period and a stable code that callers can rely on.
- First step
- Treat the class as part of the API, then ask whether the change is even right. Text where a number belongs is the wrong kind of value, which is exactly what TypeError means, so the current class is correct. Next, list how callers depend on it: instanceof checks, tests that assert the class, dashboards that group errors by name.
- Follow-ups
-
- How can callers be protected from later changes of the class?
- If a class really is wrong, how do you change it without breaking the 40 services?
- What should the library document about the errors it throws?
From the lesson Built-in error types and engine messages
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Fresher to mid
A search box passes what users type to document.querySelector, and the code handles failures with
if (error instanceof SyntaxError) showHint(). Users who type "[" get no hint, and the error tracker reports a crash. Why, and how would you fix the handler?- What it tests
- Knowing that web APIs throw DOMException objects identified by name, that a DOMException named SyntaxError is not JavaScript's SyntaxError, and that user text should be escaped or validated before it becomes a selector.
- First step
- Log what was actually caught: the constructor's name and error.name. querySelector reports an invalid selector with a DOMException whose name is "SyntaxError", and a DOMException is not an instance of JavaScript's SyntaxError, so the check is false, the catch block rethrows or ignores it, and the tracker sees a crash.
- Follow-ups
-
- Should user text reach querySelector at all, and what does CSS.escape do?
- How would you test this handler in Node.js, without a browser?
- Which other web APIs report problems through DOMException names?
From the lesson Built-in error types and engine messages
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
After the front end started shipping minified bundles, the error dashboard groups production failures under names such as "t" and "x" instead of SeatTakenError and PaymentDeclinedError. What happened, and how would you fix the error classes and keep it from coming back?
- What it tests
- Knowing that minifiers rename classes, why names copied from new.target or constructor.name break, and fixing it in the code and the build, with a check that runs against the shipped bundle.
- First step
- Look at how the classes set their name. If a base class does this.name = new.target.name or reads this.constructor.name, the name is whatever the class is called in the shipped file, and minifiers rename classes to single letters. Compare one class in the source and in the built bundle to confirm it.
- Follow-ups
-
- Which part of the fix belongs in the code, and which in the build configuration?
- How would a test catch this before the next release?
- What else in a code base relies on class or function names at run time?
From the lesson Custom errors, error cause and Error.isError
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
An API client library exports HttpError. In one service, error instanceof HttpError is false for errors the client clearly threw, while the same check works in other services. Give two likely causes, and make the check reliable.
- What it tests
- Knowing that instanceof compares with one particular class object, how duplicate copies of a package, dual CommonJS and ES module builds and realms create a second class, and which checks survive that.
- First step
- Count the HttpError classes in that service. List the installed copies of the client with npm ls and its package name, and check whether some code loads it with require and other code with import, which can load two builds. Every copy defines a class of its own, and instanceof matches only one.
- Follow-ups
-
- How would you fix the dependency tree, and how would you keep it fixed?
- How can the library make its own error check work across copies?
- When does Error.isError help here, and when does it not?
From the lesson Custom errors, error cause and Error.isError
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
About 1 in 500 checkouts in production shows a total that is a few rupees off, and nobody can reproduce it on a laptop. Walk through how you would find the bug.
- What it tests
- A method for bugs that will not reproduce: collecting evidence from production, finding what the failing cases share, turning one into a local reproduction, and testing hypotheses instead of guessing.
- First step
- Turn the 1 in 500 into data before opening a debugger. Collect the failing orders with their inputs, such as the items, coupons, prices, locale and app version, and compare them with orders that came out right. Whatever all the failing ones share and the others lack is the first hypothesis to reproduce locally.
- Follow-ups
-
- What would you record so that the next wrong total can be replayed?
- The failing orders all use one coupon type, yet that coupon works locally. What do you check next?
- How do you make sure the fix really removed the bug?
From the lesson Debugging in DevTools and Node
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 3 minutes
-
Mid to senior
To debug a staging service in a container, a teammate starts Node.js with --inspect=0.0.0.0:9229 and publishes the port so that their laptop can attach. What is the risk, and how would you debug that service safely?
- What it tests
- Knowing that the inspector allows running any code in the process, why a public binding is dangerous, and the safe alternatives: tunnels and port forwarding, opening the inspector on demand, and keeping it out of production.
- First step
- State what the port grants: any client that connects to the inspector can run arbitrary code inside the process, with its secrets and its network access, and the inspector asks for no password. Then check who can reach it: a container port published on 0.0.0.0 is open to anything that can reach the host.
- Follow-ups
-
- How do you attach from a laptop without exposing the port?
- How can you debug a process that was started without --inspect?
- What would stop an open inspector from ever reaching production?
From the lesson Debugging in DevTools and Node
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