Skip to content

Instantly share code, notes, and snippets.

@anthonyalvarez
Last active October 1, 2018 12:09
Show Gist options
  • Select an option

  • Save anthonyalvarez/cfc20ae7546cc5a839fa50d3e30b4881 to your computer and use it in GitHub Desktop.

Select an option

Save anthonyalvarez/cfc20ae7546cc5a839fa50d3e30b4881 to your computer and use it in GitHub Desktop.
Chrome DevTools Debugging JavaScript

Chrome DevTools 101: Debugging JavaScript

  • To open DeVTools on Windows: Press Ctrl + Shift + J
  • Click sources tab to debug JavaScript

The 3 parts of the Sources panel UI

  1. The File Navigator pane. Every file that the page requests is listed here.
  2. The Code Editor pane. After selecting a file in the File Navigator pane, the contents of that file are displayed here.
  3. The JavaScript Debugging pane. Various tools for inspecting the page's JavaScript. If your DevTools window is wide, this pane is displayed to the right of the Code Editor pane.

Breakpoint types

  • Line number: click line number in code editor page to set breakpoint in code. Line-of-code breakpoints are the most common type of breakpoint. When you've got a specific line of code that you want to pause on, use a line-of-code breakpoint.
  • Fetch/AJAX
  • DOM
  • Global
  • Event: Event listener breakpoints let you stop JavaScript code on event

Code stepping controls

One common cause of bugs is when a script executes in the wrong order. Stepping through your code enables you to walk through your code's execution, one line at a time, and figure out exactly where it's executing in a different order than you expected.

  1. Resume Script (F8) - The script continues executing until it reaches until next breakpoint.
  2. Step OVER next Function (F10) - confident a function is working. Function executes but you do not step into it line by line. Runs function without detailed line by line analysis. Executes a function without stepping into it.
  3. Step INTO next Function (F11) - to step through the execution of a function, one line at a time.
  4. Step OUT of next Function (Shift + F11) - if function is not relevant. DevTools will run rest of function without line by line analysis
  5. Step (F9)
  6. Deactivate Breakpoints (Shift + F8)
  7. Pause on Exceptions

Call Stack

In JavaScript Debugging pane, call stack shows list of functions that caused current function to run.

Scope Sections

When you're paused on a line of code, the Scope pane shows you what local and global variables are currently defined, along with the value of each variable. It also shows closure variables, when applicable. Double-click a variable value to edit it. When you're not paused on a line of code, the Scope pane is empty.

Watch sections

The Watch Expressions tab lets you monitor the values of variables over time. As the name implies, Watch Expressions aren't just limited to variables. You can store any valid JavaScript expression in a Watch Expression. For example: "typeof(x)."

Console

In addition to viewing console.log() messages, you can also use the Console to evaluate arbitrary JavaScript statements. In terms of debugging, you can use the Console to test out potential fixes for bugs. When paused on a line of code, Console has access to all variables currently in scope.

Edit code directly in DevTools

You've found a fix for the bug. All that's left is to try out your fix by editing the code and re-running the demo. You don't need to leave DevTools to apply the fix. You can edit JavaScript code directly within the DevTools UI.

  1. Make change in code editor and press: CTRL + S.
  2. Deactivate breakpoints to run app
  3. Resume script execution

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment