(Identify, inspect, and fix issues in your JavaScript code with confidence)
Debugging is the process of finding and fixing errors (bugs) in your code. JavaScript offers several tools and techniques to help you understand what's going wrong and why.
The simplest and most common way to debug:
const total = calculateTotal(100, 5);
console.log("Total:", total);console.table() to display data in a table.console.warn("This is a warning");
console.error("This is an error");
console.info("Some information");
console.table([{name: "Alice", age: 25}, {name: "Bob", age: 30}]);Most browsers (Chrome, Firefox, Edge) have built-in DevTools.
Use the Sources tab to:
You can:
Insert the debugger statement in your code to trigger a breakpoint:
function calculate(a, b) {
debugger; // Execution pauses here
return a + b;
}Open DevTools and reload the page — your code will pause at this line.
Use try...catch to handle and inspect runtime errors gracefully:
try {
riskyFunction();
} catch (error) {
console.error("Error caught:", error.message);
}Add optional finally for cleanup code that always runs:
finally {
console.log("Cleaning up...");
}When an error occurs, JavaScript shows a stack trace:
TypeError: Cannot read properties of undefined
at doSomething (app.js:12)
at main (app.js:20)This tells you:
Use this to trace back to the bug source.
In browser DevTools:
If you're fetching data from a server:
console.log() and DevTools for simple debugging.debugger and breakpoints let you pause and step through code.console.log() to trace the issue.debugger and use Chrome DevTools to pause and inspect.try...catch.fetch() request and inspect it via the Network tab.console.table() to debug an array of objects.