sodcalculator

10 Tech How-To

Debug JavaScript in Browser Developer Console

Guide 10-43230 · Updated · Step by step

This guide shows you how to use your browser’s developer console to find and fix JavaScript errors, inspect variables, and test code live. You’ll learn the essential commands and workflows that work in Chrome, Firefox, Edge, and Safari. No prior debugging experience is needed, just a browser and a web page with JavaScript you want to inspect.

What You’ll Need

  • A modern web browser (Chrome, Firefox, Edge, or Safari)
  • A web page that uses JavaScript (your own code or any public site)
  • Basic familiarity with opening a browser’s developer tools (usually F12 or right-click → Inspect)

Step-by-Step

  1. Open the developer console. Right-click anywhere on the page and select “Inspect” or “Inspect Element.” In the panel that opens, click the “Console” tab. Alternatively, press F12 (Windows/Linux) or Cmd+Option+J (Mac) to jump directly to the console.
  2. Look for red error messages. The console displays errors, warnings, and informational messages. Errors appear in red with an icon. Click the error text to expand it and see the full stack trace, which shows the sequence of function calls that led to the problem.
  3. Read the error message carefully. The first line tells you the type of error (e.g., “TypeError”, “ReferenceError”) and a description. The stack trace below shows the file name and line number where the error occurred. Click the file link to jump to the source in the “Sources” or “Debugger” tab.
  4. Use console.log() to inspect values. In your JavaScript code, add console.log(variableName) at points where you want to see what a variable contains. Refresh the page and the values will appear in the console. You can log multiple values at once: console.log('count:', count, 'items:', items).
  5. Use console.table() for arrays and objects. When you have an array of objects, console.table(arr) displays it as a sortable table, making it much easier to spot patterns or missing data than a plain object dump.
  6. Set breakpoints to pause execution. Go to the “Sources” (Chrome/Edge) or “Debugger” (Firefox) tab, find your JavaScript file, and click the line number where you want execution to pause. A blue arrow appears. Refresh the page; execution stops at that line.
  7. Step through your code. When paused at a breakpoint, use the controls in the debugger panel: “Step over” (F10) runs the current line and moves to the next; “Step into” (F11) enters a function call; “Step out” (Shift+F11) exits the current function. Watch the “Scope” or “Variables” panel to see current variable values.
  8. Evaluate expressions in the console while paused. With a breakpoint active, type any expression into the console and press Enter. You can read or modify variables, call functions, or test conditions. For example, type items.length to see the array length, or items.push('new') to modify it.
  9. Use the “Watch” panel. In the debugger, click the “+” in the Watch panel and type an expression like total / count. It updates live as you step through code, so you can see how a calculation changes.
  10. Test code directly in the console. You don’t need to edit your file to try something. Type any JavaScript expression at the console prompt and press Enter. For example, type 2 + 2 to see 4, or document.title to see the page title. You can also define functions and variables that persist for the current session.
  11. Filter console output. Use the filter box at the top of the console to show only errors, only warnings, or only messages from a specific file. This is useful when a page logs a lot of noise.
  12. Clear the console. Click the broom icon or press Ctrl+L (Windows/Linux) or Cmd+K (Mac) to clear all messages and start fresh.

Common Mistakes

  • Forgetting to refresh after editing. The console shows the state of the page as loaded. If you edit your JavaScript file, you must refresh the page to see the new code run.
  • Logging objects, not their properties. console.log(obj) shows a reference that updates live, which can be confusing. Use console.log(JSON.stringify(obj)) to capture a snapshot, or log specific properties like obj.name.
  • Using console.log on undefined variables. This throws a ReferenceError. Check the variable name for typos and ensure it’s in scope at that point.
  • Setting breakpoints in minified code. Production files are often minified (one long line). The debugger still works, but it’s much harder to read. Use the “Pretty Print” button (curly braces icon) in the Sources tab to format the code.
  • Ignoring warnings. Yellow warnings often point to real issues like deprecated APIs or potential bugs. Read them even if the page seems to work.
  • Assuming the console is for output only. It’s a two-way REPL. You can run any JavaScript, not just view logs.

Tips

  • Use debugger; statement in your code. Adding the line debugger; in your JavaScript acts like a manual breakpoint. When the browser hits it with dev tools open, execution pauses automatically.
  • Log with context. Instead of console.log(total), write console.log('total after discount:', total). This makes multi-line logs much easier to scan.
  • Group related logs. Use console.group('Checkout') and console.groupEnd() to nest logs under a collapsible heading.
  • Use console.assert() for conditional logging. console.assert(condition, 'message') only logs the message if the condition is false. This is great for checking assumptions without cluttering the console.
  • Copy objects to clipboard. Right-click on a logged object in the console and select “Copy object” to paste it as JSON into your editor for closer inspection.
  • Learn keyboard shortcuts. Ctrl+Shift+J (Windows/Linux) or Cmd+Option+J (Mac) opens the console directly. F8 pauses/resumes script execution when a breakpoint is active.

Troubleshooting

  • The console is empty even though I expect errors. Check that you’re on the right page and that JavaScript actually runs. Try typing console.log('test') — if nothing appears, the console itself may be filtered. Click the filter icon and ensure “Errors” and “Info” are enabled.
  • My breakpoint never triggers. Verify the file you’re viewing is the one being executed (check the network tab for the correct file name). Also ensure you refreshed the page after setting the breakpoint. If the code runs in an event handler (like a button click), you need to trigger that event, not just reload.
  • The error points to a line that seems fine. The line number in the stack trace refers to the state of the file when it was loaded. If you’ve edited the file since, the line numbers may have shifted. Refresh the page to reload the current version.
  • Variables show as “undefined” in the Scope panel. This usually means the variable isn’t in scope at that breakpoint. Check that you’re inside the correct function or block. If the variable is defined later in the function, step forward to see it.
  • Console commands don’t work on the page. Some sites use Content Security Policy (CSP) that restricts what you can do from the console. You’ll see a warning. You can still view logs and set breakpoints, but direct DOM manipulation may be blocked.
  • Performance is slow while debugging. Breakpoints and heavy logging can slow down execution. Remove unused breakpoints, reduce console.log calls, and close the dev tools when not actively debugging.

Frequently Asked Questions

Q: How do I open the console in Safari?
A: Go to Safari → Preferences → Advanced and enable “Show Develop menu in menu bar.” Then Develop → Show JavaScript Console, or press Cmd+Option+C.

Q: Can I use the console to modify the page content?
A: Yes. For example, typing document.body.style.backgroundColor = 'red' changes the background. These changes are temporary and reset on refresh.

Q: What’s the difference between console.log and console.error?
A: Both print to the console, but console.error formats the message in red and includes a stack trace. Use it for actual errors, not for general debugging info.

Q: Why does my console.log show an old value after I change the variable?
A: When you log an object, the console shows a live reference. By the time you expand it, the value may have changed. Log a primitive value (like obj.count) or use JSON.stringify(obj) to capture a snapshot.

Q: How do I debug asynchronous code like fetch or setTimeout?
A: Use breakpoints inside the callback or the .then() block. You can also check the “Async” checkbox in the debugger’s call stack panel to see the full async context. Logging at the start of the callback is often the simplest approach.

Q: Can I save console output to a file?
A: In Chrome, right-click in the console and select “Save as…” to download the log. In Firefox, you can copy the entire console content with right-click → Select All, then copy and paste into a text editor.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →