JavaScript Debugging Interview Questions and Answers
Practice diagnosing JavaScript behavior with breakpoints, browser developer tools, and useful console methods.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What is debugging in JavaScript?
- What are Chrome DevTools and how do you use them for debugging?
- What is a breakpoint and how do you add one?
- What is the difference between console.log(), console.warn(), and console.error()?
- What does a debugger breakpoint do?
- What is the primary purpose of console.log() in JavaScript? How does it differ from other methods like console.error() or console.warn()?
- What is the difference between a ReferenceError and a TypeError?
Theoretical JavaScript Interview Questions
console.log() is your everyday debugging buddy - it prints general info to the console. But the console object has more targeted tools: console.error() shows messages in red, making errors jump out immediately. console.warn() uses yellow for things that aren't broken yet but could be. The practical difference matters in production logging too, since log aggregators can filter by severity level. Pro tip: console.table() is great for arrays and objects when you want a quick visual.
console.log() shines when you need to peek inside your code while it runs. Drop it before and after a suspicious function to track variable values, confirm which branch of an if-statement executed, or inspect the shape of an API response. It's especially handy with async code where the execution order might surprise you. That said, don't ship console.log calls to production - use a proper logger instead, and lean on browser DevTools breakpoints for complex debugging sessions.
Debugging is finding and fixing the bugs in your code - the mismatch between what you think the code does and what it actually does. Start with console.log to inspect values, use the browser's debugger for anything more complex, and read error messages carefully since they usually point you directly to the problem. The best debugging skill is forming a hypothesis about what's wrong and systematically testing it.
function add(a, b) {
return a - b; // bug: should be +
}
console.log(add(3, 4)); // -1 (wrong, expected 7)
// After fix:
function add(a, b) {
return a + b;
}
console.log(add(3, 4)); // 7
Chrome DevTools is the browser's built-in developer toolkit, opened with F12 or right-click and Inspect. For JavaScript, the Console and Sources panels are your main tools. Console lets you run JS snippets and see errors and logs. Sources lets you set breakpoints and step through code line by line. The Network tab helps when debugging API calls or missing assets.
- Console: view errors, warnings, and log messages; run JavaScript interactively.
- Sources: view source files, add breakpoints, and step through code line by line.
- Network: inspect HTTP requests and responses.
A breakpoint pauses code execution at a specific line so you can inspect variable values, the call stack, and scope chain. Click a line number in the DevTools Sources panel to set one, or write debugger; in your code to do the same thing when DevTools is open. Once paused, you can step through code line by line, step into function calls, or run to the next breakpoint.
function calculateTotal(price, tax) {
debugger; // pauses here when DevTools is open
return price + tax;
}
calculateTotal(100, 18);
All three write to the browser console, but they serve different purposes and render with different visual styles. Use log for general info, warn for potential problems that aren't fatal, and error for actual failures - this also lets you filter console output by severity in DevTools.
- console.log(): prints general information (white/default styling).
- console.warn(): prints a warning with a yellow background - use it for non-critical issues that might indicate a problem.
- console.error(): prints an error with a red background and a stack trace - use it for actual failures. The distinction matters when filtering Console output by severity.
- console.table() is also worth knowing - it displays arrays and objects in a readable grid format.
console.log("User loaded"); // general info
console.warn("Cache is almost full"); // warning
console.error("Failed to fetch data"); // error with stack trace
A timer callback runs later, after the surrounding try block has finished. That later callback is outside the original call stack, so handle its errors inside the callback or convert the work to a Promise and handle its rejection.
A ReferenceError means the variable doesn't exist at all - you never declared it, or it's out of scope. A TypeError means the variable exists but you're using it in a way its type doesn't support - like calling a number as a function, or reading a property off null. These are the two most common runtime errors - learn to recognize them and you'll fix bugs much faster.
// ReferenceError
console.log(foo); // ReferenceError: foo is not defined
// TypeError
let num = 42;
num(); // TypeError: num is not a function
let obj = null;
console.log(obj.name); // TypeError: Cannot read properties of null
A breakpoint pauses execution at a chosen point so you can inspect the call stack, variables, and program state. It helps reveal behavior without changing the program's logic.
A small, reliable reproduction removes unrelated behavior and makes the cause easier to isolate. It also provides a repeatable check that a fix works.
Source maps connect generated or bundled code to its original files and locations. Developer tools can then show useful breakpoints and stack traces in the original sources.
A watch expression displays a selected expression's value while execution is paused. Prefer side-effect-free expressions so inspection does not change program state.
An asynchronous callback may run after the original stack has unwound, so an ordinary trace may not show the full causal path. Developer tools can preserve async context, but timers and external events still add boundaries.
Repeatedly disable or revert about half the suspected changes and test whether the problem remains, then continue with the half containing the regression. This is most effective with a deterministic reproduction and known-good baseline.
A Heisenbug changes behavior when observed, often because instrumentation affects timing or shared state. Use repeatable, minimally intrusive measurements and compare timing and state under production-like conditions.