๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท๐Ÿ‡ฌ๐Ÿ‡ง Limited Time โ€” UK Onlyยท๐ŸŽ“ Free Learning for 1 Monthยท๐Ÿค– Free AI Training Includedยท๐Ÿ“š 4,000+ Lessons ยท 35,000+ Quizzesยท๐Ÿ† GCSE Mocks ยท Olympiad Papersยทโšก Selected Students Only ยท Limited Placesยท๐ŸŽ Free Value Worth ยฃ2,000ยท
โšก JavaScript

Debugging Basics

๐Ÿ“š What is JavaScript Debugging? Debugging in JavaScript means finding and fixing errors in your code. Browser DevTools is your best friend: open with F12 (or right-click > Inspect), find the Console tab, and you see all console.log() output, error messages, and stack traces.

8 min 10 XP Lesson 9 of 21
Debugging Basics
๐ŸŒ

Appy Saysโ€ฆ

Every developer spends as much time debugging as writing code. The devs who ship fast aren't the ones who write perfect code first time โ€” they're the ones who find and fix bugs fastest. The browser's DevTools is your X-ray machine.

๐Ÿ“–

What is JavaScript Debugging?

Debugging is finding and fixing errors in your code. JavaScript errors appear in the browser's Console (DevTools). You can also pause execution and inspect variables using breakpoints.

  • โ€ขOpen DevTools: F12 (or Cmd+Option+I on Mac) โ†’ Console tab
  • โ€ขconsole.log(value) โ€” print any value to the console
  • โ€ขconsole.error(msg), console.warn(msg) โ€” coloured output
  • โ€ขconsole.table(array) โ€” display arrays/objects as a table
  • โ€ขRed error messages show the file, line number, and error type
  • โ€ขBreakpoints: click a line number in Sources tab to pause execution
  • โ€ขdebugger; statement in code pauses execution (same as a breakpoint)
๐ŸŽฎ

Think of it like spectator mode in a game

Spectator mode in Minecraft lets you fly through the world and see what's happening without changing anything. A breakpoint does the same for your code โ€” execution freezes mid-run and you can inspect every variable's value at that exact moment.

โš™๏ธ

How It Works

  • โ€ข1. Open DevTools โ†’ Console to see errors and console.log output
  • โ€ข2. Click the error message to jump to the exact line that failed
  • โ€ข3. Add console.log(variableName) before the suspect line to inspect values
  • โ€ข4. In the Sources tab, click a line number to set a breakpoint
  • โ€ข5. When execution hits the breakpoint, it pauses โ€” hover variables to see their values
  • โ€ข6. Use Step Over (F10), Step Into (F11), Step Out (Shift+F11) to advance line-by-line
๐ŸŒ

Real-World Examples

  • โ€ขA login form not submitting โ€” console shows TypeError: Cannot read properties of undefined
  • โ€ขAn API fetch returning wrong data โ€” console.log(response) reveals the actual shape
  • โ€ขA counter not incrementing โ€” breakpoint shows the variable was never re-assigned
  • โ€ขSpotify's engineers use breakpoints daily when player state gets out of sync with the UI
  • โ€ขTikTok's team uses console.table(posts) to verify feed data before rendering
๐Ÿ’ก

Key Facts

  • โ€ขThe most common JS errors: TypeError (wrong type), ReferenceError (undefined variable), SyntaxError (typo in code)
  • โ€ขChrome DevTools has a network tab showing every API request โ€” invaluable for fetch debugging
  • โ€ขYou can run JS directly in the Console: type any expression and press Enter
  • โ€ขThe debugger; statement only pauses if DevTools is open โ€” safe to commit temporarily
โš ๏ธ

Watch Out!

Remove all console.log() and debugger; statements before shipping to production. They slow things down and expose your internal data to users who open DevTools. Use a linter rule (no-console) to catch forgotten logs.

๐Ÿ“Œ

Remember

F12 โ†’ Console = your best friend. Read the red error message first โ€” it tells you the type, file, and line. Add console.log() around the problem to trace values.

โœ…

What You Learned

  • โ€ขDevTools Console shows errors and console.log output with file/line info
  • โ€ขBreakpoints pause execution so you can inspect variable values mid-run
  • โ€ขUnlocks: finding any bug faster, understanding complex control flow, confident production code

Key Facts

  • โ†’The most common JS errors: TypeError (wrong type), ReferenceError (undefined variable), SyntaxError (typo in code)
  • โ†’Chrome DevTools has a network tab showing every API request โ€” invaluable for fetch debugging
  • โ†’You can run JS directly in the Console: type any expression and press Enter
  • โ†’The debugger; statement only pauses if DevTools is open โ€” safe to commit temporarily

Real-World Examples

โ€ข A login form not submitting โ€” console shows <code>TypeError: Cannot read properties of undefined</code> โ€ข An API fetch returning wrong data โ€” <code>console.log(response)</code> reveals the actual shape โ€ข A counter not incrementing โ€” breakpoint shows the variable was never re-assigned โ€ข Spotify's engineers use breakpoints daily when player state gets out of sync with the UI โ€ข TikTok's team uses <code>console.table(posts)</code> to verify feed data before rendering

Remember

F12 โ†’ Console = your best friend. Read the red error message first โ€” it tells you the type, file, and line. Add console.log() around the problem to trace values.

Quick Quiz

1 / 2

What is debugging?