Back to KB
Difficulty
Intermediate
Read Time
4 min

How to Debug JavaScript Like a Pro

By Codcompass TeamΒ·Β·4 min read

Stop using console.log for everything. Here's a better way.

The Mindset

Debugging is not guessing.
Debugging is the scientific method:
1. Observe (what's happening?)
2. Hypothesize (what could cause it?)
3. Test (prove or disprove)
4. Fix (when you know the cause)

Enter fullscreen mode Exit fullscreen mode

1. Chrome DevTools You're Not Using

// Breakpoints (better than console.log!)
// Sources tab β†’ click line number β†’ red dot
// Code pauses here β†’ inspect all variables in Scope panel

// Conditional breakpoints
// Right-click breakpoint β†’ "Edit breakpoint"
// Enter: data.length === 0
// Only pauses when condition is true!

// Logpoint (no pause, just logs!)
// Right-click line β†’ "Add logpoint"
// Enter: 'data:', data
// Logs expression value without stopping execution

// DOM breakpoints
// Elements tab β†’ right-click element β†’ Break on:
//   - subtree modifications
//   - attribute modifications
//   - node removal

// XHR/Fetch breakpoints
// Sources panel β†’ XHR/fetch breakpoints
// Add: /api/users  β†’ breaks on any request containing this string

// Event listener breakpoints
// Sources panel β†’ Event Listener Breakpoints
// Check "click" β†’ breaks on any click event anywhere

Enter fullscreen mode Exit fullscreen mode

2. The Console Is Your REPL

// You can run ANY JavaScript in the console
// While paused at a breakpoint, you have access to ALL variables in scope!

// At breakpoint:
data.length           // Check array length
JSON.stringify(data)  // Pretty-print object
data.map(d => d.id)   // Transform and inspect

// Use $_ to reference last result:
[1, 2, 3].map(x => x * 2)  // [2, 4,6]
$_                           // [2, 4, 6]
$_.length                    /

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back