Skip to content

Instantly share code, notes, and snippets.

@jasonfigueroa
Last active September 17, 2022 22:56
Show Gist options
  • Save jasonfigueroa/9d39e0bb21a986fdfe6a7754d2627c84 to your computer and use it in GitHub Desktop.
Save jasonfigueroa/9d39e0bb21a986fdfe6a7754d2627c84 to your computer and use it in GitHub Desktop.
How to Debug JavaScript in Visual Studio Code

How to Debug JavaScript in Visual Studio Code

Required software:

  • Google Chrome
  • Visual Studio Code (VS Code)
  • Live Server Extension for VS Code

Configure the Debugger

Note: The following is a screenshot of the Live Server Extension.

Live Server extension for VS Code

Once installed click on the Go Live button at the bottom right.

Go Live button

You should see a message similar to the following.

Live Server started message

Note: The port number can be changed in the settings.json file, in the .vscode directory.

Click on the Run and Debug button in the side bar.

Run and Debug button in VS Code

Click on the create a launch.json file link.

create a launch.json file link

Click on the Web App (Chrome) debugger option.

Web App (Chrome) debugger option

A launch.json file will be generated and stored in the .vscode directory.

The value for the name property can be changed to whatever you like. It will be displayed here.

name in the Run and Debug dropdown

The value for the file property should be the correct path to your project's index.html file. Please ignore the path in the screenshot, these screenshots were taken from a windows machine

Debugging

Open the js file and place a breakpoint somewhere by clicking just to the left of a number.

Place a breakpoint

Click, once again, on the Run and Debug button in the sidebar and then click on the Play button.

Debugger Play button

A new browser window should open and the debugger should pause on the breakpoint.

Debugger stopped on breakpoint

Note: Notice the variable information on the left in the screenshot above.

The following are the controls for the debugger. From left to right you have the following buttons: Continue, Step Over, Step Into, Step Out, Restart and Stop.

Debugger controls

If the line the debugger is paused on is calling a function and you click the Step Into button, the debugger will step into the function code. If instead you click the Step Over button, the debugger will step over the function call and continue onto the next line.

To add a variable to the Watch pane highlight it, right click it and click on Add to Watch.

Right click Add to Watch

Add variable to Watch

To remove a variable from the Watch pane click on the x button to the right.

Remove variable from Watch

Note: You can also add objects to the Watch pane.

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