Getting Started with Debugging in VS Code (Official Beginner Guide)

Visual Studio Code
1 Jul 202413:00

Summary

TLDRThis video tutorial showcases essential debugging techniques in Visual Studio Code (VS Code). It covers setting breakpoints, using the debug toolbar, and navigating through variables, watch, call stack, and loaded scripts. The tutorial also highlights debugging a C project with extensions and demonstrates polymorphism. Further, it explores debugging a React web app, manipulating variables, and using developer tools within VS Code, including the unique Edge developer tools integration.

Takeaways

  • 🔧 Debugging is a crucial skill for developers, and VS Code provides robust tools to facilitate this process.
  • 💻 For simple JavaScript and Node.js files, VS Code's debugging capabilities work out of the box.
  • 🛑 Adding breakpoints in VS Code is straightforward; just click on the line number where you want the code execution to pause.
  • 📚 The debug sidebar in VS Code includes sections for variables, watch, call stack, and loaded scripts, providing comprehensive debugging information.
  • ⏯ The debug toolbar offers buttons for continuing, stepping over, stepping into, and stepping out of methods, as well as restarting and stopping the debugging process.
  • 🧩 When working with unsupported languages like C, installing the appropriate extensions such as C/C++ and C++ Build Tools is necessary for debugging.
  • 📝 The 'watch' section is particularly useful for tracking the values of specific expressions and variables throughout the debugging session.
  • 🔄 Debugging a React web app in VS Code requires configuring the 'launch.json' file to point to the correct port and set other parameters like browser type and web root.
  • 🎛️ During debugging, you can manipulate variables and use the console to output information, which is helpful for testing specific conditions.
  • 🌐 The debug console allows access to the window object and DOM elements, enabling real-time manipulation of the web page within VS Code.
  • 🔧 Edge browser users can access the Edge Developer Tools directly within VS Code for a more integrated debugging experience.

Q & A

  • What is one of the most important skills for developers according to the video?

    -Debugging is one of the most important skills for developers.

  • How does the debugging process start in Visual Studio Code (VS Code)?

    -The debugging process in VS Code can be started by clicking the 'Run and Debug' icon on the left-hand side or by pressing F5.

  • What is the purpose of setting a breakpoint in VS Code?

    -A breakpoint is used to pause the execution of the code at a specific line, allowing developers to inspect variables and the flow of the program.

  • What are the different sections visible in the left pane during debugging in VS Code?

    -During debugging in VS Code, the left pane shows sections for Variables, Watch, Call Stack, and Loaded Scripts.

  • What does the Variables section display in the debugging pane of VS Code?

    -The Variables section displays the variables in the current debugging scope, including their types and values.

  • How can you track specific expressions or variables during a debugging session in VS Code?

    -You can track specific expressions or variables by adding them to the Watch section in the debugging pane.

  • What is the difference between the 'Step Over' and 'Step Into' buttons in the debug toolbar of VS Code?

    -The 'Step Over' button allows you to execute the current method without stepping into its internal calls, while the 'Step Into' button lets you step into a method to debug its internal calls.

  • What is the use of the 'Loaded Scripts' section during debugging in VS Code?

    -The 'Loaded Scripts' section lists all the scripts that have been loaded in the current debugging session, which is useful for navigating different parts of the code or libraries being used.

  • Why might you need to install extensions to debug a C project in VS Code?

    -To debug a C project in VS Code, you might need to install extensions like the C/C++ extension pack, which includes the C/C++ compiler and debugger tools.

  • How can you set a conditional breakpoint in VS Code?

    -You can set a conditional breakpoint by right-clicking on the breakpoint and specifying a condition, such as when a variable equals a certain value.

  • What is the purpose of the 'Restart' button in the debug toolbar of VS Code?

    -The 'Restart' button is used to restart the debugging session from the beginning, which is useful when you want to start over without having to stop and relaunch the debugger.

  • How can you access the browser's developer tools within VS Code when using Edge?

    -You can access the Edge developer tools within VS Code by installing the Microsoft Edge Tools for VS Code extension and then clicking on the 'Edge Developer Tools' option in the debug toolbar.

  • What is the significance of the 'Launch.json' file in debugging a web app in VS Code?

    -The 'Launch.json' file is significant because it configures the debugging environment, including the port number, browser type, and other parameters necessary for the debugger to attach to the web app.

Outlines

00:00

🛠️ Debugging in VS Code

The paragraph introduces debugging in Visual Studio Code (VS Code) as a crucial skill for developers. It explains how to set breakpoints in JavaScript and Node.js projects directly within the editor. The process of starting a debug session is outlined, including navigating through the interface and toolbar options such as 'Continue', 'Step Over', 'Step Into', 'Step Out', 'Restart', and 'Stop'. The sidebar sections like 'Variables', 'Watch', 'Call Stack', and 'Loaded Scripts' are highlighted for their utility in debugging. The paragraph also touches on debugging a C project by installing necessary extensions and setting up the environment.

05:01

🔍 Advanced Debugging Techniques

This section delves into more advanced debugging techniques, such as manipulating variables and setting conditions for breakpoints. It discusses how to use the 'Watch' section to monitor variable values and how to modify these values during a debugging session. The concept of stepping into and out of methods is demonstrated, along with the use of the call stack to understand the flow of execution. The paragraph also shows how to set breakpoints based on conditions, such as a specific employee record, and how to interact with the console and the window object to debug a React web application.

10:02

🌐 Debugging Web Apps with Edge

The final paragraph focuses on debugging a web application using Microsoft Edge within VS Code. It emphasizes the importance of configuring the 'launch.json' file correctly to target the right port and other necessary parameters. The ability to manipulate variables and elements directly from the debug console is highlighted, showcasing the power of live editing during a debug session. Additionally, the paragraph introduces the integration of Edge Developer Tools within VS Code, allowing for direct manipulation of the DOM and other browser functionalities. The video concludes with a call to action for further content and a prompt for viewer engagement.

Mindmap

Keywords

💡Debugging

Debugging is the process of identifying and correcting errors or bugs in computer programs. In the context of the video, debugging is a crucial skill for developers using Visual Studio Code (VS Code). The script describes how to debug JavaScript and Node.js files, set breakpoints, and use the debug toolbar to step through code to find and fix issues. Debugging is central to the video's theme of improving developer efficiency and code quality.

💡VS Code

Visual Studio Code (VS Code) is a popular, open-source code editor developed by Microsoft. It is mentioned extensively in the script as the primary tool for demonstrating debugging techniques. VS Code supports debugging for many languages and platforms, and the video showcases its debugging capabilities, emphasizing its importance in a developer's workflow.

💡Breakpoints

A breakpoint is a marker in the code that tells the debugger to pause execution at a specific line. The script explains how to set breakpoints in VS Code by clicking on the line number, which is vital for pausing the program at a desired point to inspect variables and execution flow. Breakpoints are a fundamental part of the debugging process demonstrated in the video.

💡Variables

Variables in programming are used to store data values. The script mentions the 'Variables' section in the debug panel of VS Code, which displays the current scope's variables, including their types and values. Understanding and inspecting variables is crucial for debugging, as it helps developers track data changes and identify unexpected values.

💡Watch

In the context of debugging in VS Code, 'Watch' refers to a feature that allows developers to monitor the values of specific expressions or variables throughout a debugging session. The script illustrates how to use the 'Watch' feature to track variables like 'salary' and 'person', which is essential for observing how data changes as the program executes.

💡Call Stack

The call stack is a data structure that keeps track of active subroutines or functions during execution. In the script, the 'Call Stack' section in the debug panel is highlighted, showing the hierarchy of function calls and the path the program took to reach the current point. This is crucial for understanding the flow of execution and for navigating through nested function calls.

💡Polymorphism

Polymorphism is an object-oriented programming concept where objects of different classes exhibit the same interface. The script uses a C project to demonstrate polymorphism by determining a worker's salary based on whether they are an employee or a contractor. This concept is used to show how debugging can be applied to understand complex behaviors like polymorphism.

💡Extensions

Extensions in VS Code are add-ons that provide additional functionality or support for different programming languages and tools. The script discusses installing the C/C++ extension and the net install tool to debug a C project, emphasizing the importance of extensions for enhancing the debugging capabilities of VS Code beyond its default configuration.

💡Launch Configuration

Launch configuration in VS Code is a set of settings that define how the debugger should start the application being debugged. The script mentions configuring 'launch.json' for a React web app, specifying parameters like port, request type, and URL. This configuration is essential for setting up the correct environment for debugging web applications.

💡React

React is a popular JavaScript library for building user interfaces, particularly for single-page applications. The script includes a demonstration of debugging a React web app, focusing on a CRUD (Create, Read, Update, Delete) application for employee management. React's component-based architecture is used to illustrate how to debug and manipulate UI states and interactions.

💡Developer Tools

Developer Tools are a set of utilities integrated into web browsers and development environments like VS Code to help developers test, debug, and profile their web applications. The script shows how to access Edge Developer Tools within VS Code, allowing for in-browser debugging and manipulation of DOM elements directly from the editor. This integration enhances the debugging experience by combining the power of browser tools with the convenience of VS Code.

Highlights

Debugging is one of the most important skills for developers.

VS Code supports debugging for JavaScript and Node.js out of the box.

Adding a breakpoint in VS Code is as simple as clicking on the line number.

The Run and Debug icon starts the debugging process.

Debugging tools in VS Code include variables, watch, call stack, and loaded scripts.

The Debug Toolbar offers options to continue, step over, step into, step out, restart, and stop debugging.

For unsupported languages, install the necessary extensions from the Marketplace.

The C Dev Kit and C extensions are needed for C language debugging.

Demonstrates polymorphism in a C project using VS Code's debugging tools.

Variables and watch expressions can be monitored during a debugging session.

The call stack shows the hierarchy of function calls during execution.

Loaded scripts list all scripts loaded in the current debugging session.

The debug console allows for quick value checks and manipulation.

React web apps can be debugged to manipulate variables and view changes in real-time.

Breakpoints can be set with conditions to hit only under specific circumstances.

The terminal in VS Code can be used to write to the console and check values.

The debug console provides access to the window object and other DOM elements.

Edge Developer Tools can be accessed within VS Code for debugging web apps.

The developer tools allow for real-time manipulation of page elements.

VS Code's debugging documentation offers more in-depth information and guides.

Encourages viewers to request more videos on debugging topics and engage with the content.

Transcripts

play00:00

hey everyone if you'd like to keep up

play00:02

Tod dat with a developer best practices

play00:05

and tips in Tech check out GitHub

play00:08

Insider newsletter via the link in the

play00:11

video description below as a developer

play00:14

debugging is one of the most important

play00:16

skills to have and in vs code there's so

play00:19

much that you can do so let's go ahead

play00:21

and take a look so the great thing when

play00:23

debugging with vs code is that for

play00:25

simple files using JavaScript and node

play00:28

it works right out the box in this

play00:30

simple program here that prints out a

play00:33

greeting to add a break point I can just

play00:35

go to the line number that I want let's

play00:37

say it was six and click on the left

play00:39

hand side here which will show a red dot

play00:42

and to begin the process of debugging on

play00:45

the left hand side you'll see there's an

play00:48

icon for run and debug and then I can

play00:51

just come over here to click run and

play00:53

debug to start the process now if it's

play00:55

your first time you may get a drop down

play00:58

like this at which point you'll just

play01:00

choose node.js now since I've already

play01:03

done that I can just go to run and debug

play01:05

and it will start the process and it

play01:07

stops at line

play01:09

six and now on the left pane you'll

play01:12

notice several sections one for

play01:14

variables which displays the variables

play01:16

in the current debugging scope including

play01:19

their types and values we have watch

play01:22

that allows you to track the values of

play01:24

specific expressions and variables

play01:26

you're interested in throughout the

play01:28

whole debugging session call stack shows

play01:30

the hierarchy of function calls at the

play01:33

current moment in the execution

play01:34

indicating the path the program took to

play01:36

reach the current point and loaded

play01:39

scripts lists all the scripts that have

play01:41

been loaded in your current debugging

play01:44

session which is useful when you're

play01:45

navigating different parts of your code

play01:47

or libraries being used and at the top

play01:49

of the screen you'll notice that there's

play01:51

a debug toolbar with a few different

play01:54

buttons the first being to continue so

play01:56

after you've stopped at a break point

play01:58

you just want to continue to the next

play02:00

breakpoint or the end of the program you

play02:01

hit this button if you come to a method

play02:03

and you don't want to jump in that

play02:05

method but step over it that's what this

play02:08

button here is for on the flip side if

play02:10

you do want to step into the method you

play02:12

hit the down arrow and then to step out

play02:14

of the method the up Arrow if you want

play02:16

to restart the debugging process you

play02:18

click restart and to stop it this red

play02:21

square here so really those are some of

play02:23

the basics right there but why don't I

play02:26

show you this toolbar in action in

play02:27

another project with more code in this

play02:30

project here it's going to demonstrate

play02:33

polymorphism and it is a c project now

play02:36

whenever you're going to to bug a

play02:39

project that's not supported by default

play02:42

in vs code you'll want to go to the

play02:44

extensions Marketplace and install the

play02:46

appropriate extensions that you need so

play02:49

in my case I needed to install the C dev

play02:53

kit which helps you manage your C code

play02:56

in addition to the C extension and last

play02:59

but not least the net install tool so

play03:01

you can set up the net runtime if you'd

play03:04

like some detailed instructions on

play03:06

setting that up you can watch the first

play03:07

two modules in this video here now this

play03:10

project here demonstrates polymorphism

play03:12

by taking a particular worker and

play03:15

determining what their salary is based

play03:18

on whether or not they are an employee

play03:20

or a contractor let's go ahead and

play03:22

choose run and debug as you can see on

play03:24

line 48 I already have a break point

play03:27

there where it will stop I'll jump at

play03:29

the very top here and click the play

play03:31

button to start the debugging process

play03:33

and soon we'll see our debug toolbar at

play03:36

the very top let's experiment with some

play03:37

of these buttons stepping over if I

play03:40

wanted to step over this method it will

play03:43

do so but let's go ahead and step into

play03:45

it so we could see a few other things so

play03:47

now I'm stepping into the determin

play03:50

weekly salary and it is for a Contractor

play03:54

on the left pane you could see variables

play03:56

that are local such as wage and we L

play04:00

hours if there were some other variables

play04:02

that I wanted to add to keep an eye on I

play04:04

can just click the plus sign under watch

play04:07

and put that there so let's say I put in

play04:09

person it does not exist in the current

play04:12

context which makes sense because we're

play04:14

not seeing it in this method right now

play04:16

but if I put in salary it's currently

play04:19

zero but as I step through now it has a

play04:22

value of 3850 so the watch section is

play04:25

very useful for variables that you want

play04:26

to keep track of when it's not defaulted

play04:29

at the very top under locals right below

play04:31

here is the call stack so we are within

play04:35

determine weekly salary so that's the

play04:38

last method that we called so that's

play04:40

going to be listed first here but the

play04:42

level above it is our main program which

play04:45

we see right below it so when we get out

play04:47

of this method we should only see one

play04:49

line which is for the main program so if

play04:52

we want to step out let's go ahead and

play04:54

click the step out button now once we

play04:56

step out you'll notice that it's written

play04:59

that the contra contractor work 55 hours

play05:01

with a total of 3850 that's appropriate

play05:03

for a contractor that has overtime

play05:06

you'll notice that on the watch now we

play05:08

have a value for a person because it is

play05:10

within context we can see it's a

play05:12

contractor we also see that our call

play05:14

stack is now down by one line because we

play05:18

are in our main method and at the bottom

play05:20

there is just a nice view of all our

play05:22

break points if I wanted to start this

play05:24

debug session over I could just click on

play05:27

restart right here and by default it

play05:30

shifts to this terminal view but I could

play05:32

click on debug console again and now

play05:36

online 48 rather than stepping into it I

play05:39

could just step over it prints out our

play05:43

results one more time and at this point

play05:46

I can either continue to the end of the

play05:49

program or just stop the debugging

play05:51

process by clicking this red square here

play05:53

which is what I'll do I should also

play05:55

mention that from this debug console you

play05:58

can check values here here quickly also

play06:01

so if I typed in wage it shows me 70 if

play06:04

I typed in person it indicates the

play06:06

object type which is a

play06:09

contractor I'd like to turn our

play06:11

attention now to another app this time

play06:14

it's a react web app that I'm going to

play06:15

go ahead and launch and we'll do a

play06:18

little debugging on this app which is a

play06:21

crud application for an employee

play06:23

management software so you can create an

play06:26

employee obviously we could read the

play06:29

record update them and delete them now

play06:31

I'd like to note that when I executed

play06:34

this it indicated that I'm using Port

play06:37

3,000 which is important to note because

play06:39

when we go to debug you want to make

play06:41

sure that you set up your configuration

play06:45

and launch. Chason to point to Port 3000

play06:48

in the URL in addition to these other

play06:50

parameters such as type request name and

play06:52

web rout and if you have access to chat

play06:55

you could simply ask it to Define what

play06:57

each of them do which is fairly

play06:58

straightforward but I do appreciate the

play07:00

reinsurance and as it mentioned type is

play07:03

for the type of browser for request we

play07:06

want to indicate launch because that's

play07:07

what we're doing launching the browser

play07:09

name pretty much anything can go in

play07:10

there it's whatever is going to be

play07:11

useful for you and for the URL this is

play07:13

where we're going to indicate our

play07:15

specific port and then of course web

play07:17

root which tells the debugger where the

play07:19

root of the web server is relative to

play07:21

your project now for my demonstration

play07:23

I'm going to be showing how to

play07:25

manipulate some variables which will be

play07:28

reflected from the the edit page so I'll

play07:30

add a breakpoint right here and I will

play07:32

start the debugging and similar to what

play07:34

I've shown before we're able to set

play07:36

break points to manipulate data but in

play07:39

this case I'm going to go ahead and

play07:41

choose edit like I mentioned and say

play07:44

that we want to change George blue to

play07:48

let's say George strong and we click

play07:52

update as you can see under our watch

play07:55

variables it indicates strong but right

play07:57

from here I can right click and either

play08:00

add an expression set value or copy the

play08:02

value let's change this and instead of

play08:05

strong we'll just say stronger and when

play08:08

we go to continue the process we don't

play08:10

have strong but we have stronger just as

play08:13

expected and it's a quite powerful thing

play08:14

to be able to manipulate the variables

play08:16

during the debugging process especially

play08:19

if there's specific conditions that you

play08:20

want to meet so let's say I wanted to

play08:23

test a particular condition but only for

play08:26

the record of Adrien orange I I can come

play08:30

here and edit this break point and

play08:33

indicate that I only want to hit this

play08:36

break point when the first name is equal

play08:38

to Adrien now if we hover over the

play08:40

breakpoint we see that it says that

play08:42

there's a condition with our expression

play08:44

so if I come back up here and change

play08:47

let's say Ral there's an interesting

play08:50

name let's change that to let's change

play08:52

that to rynal and click update it

play08:54

updates without any breako being hit but

play08:57

if I come over here to adri Adan and

play09:00

then want to change this name here to

play09:03

Smith now the breakpoint gets hit just

play09:06

as we expected and I'm not going to make

play09:08

any changes I'm just going to continue

play09:10

and the update is made another thing I'd

play09:12

like to show in this uh web app is in

play09:15

respect to the terminal if you are a fan

play09:19

of writing out to the console if you

play09:21

notice here on line 15 there's a

play09:24

console. info command that's executed

play09:27

and in fact I can add something

play09:31

additional we are in edit mode and now

play09:34

when I run the process to edit and I go

play09:38

to edit a name even if I don't make any

play09:41

changes right over here is where it will

play09:44

write out some content that I want

play09:46

specifically about the object of the

play09:48

selected employee and that we are in

play09:51

edit mode another thing to note about

play09:53

this section here is I can have access

play09:57

to the window object if I want

play09:59

and pretty much any other element if I

play10:02

wanted to go to see the values in the

play10:04

body I could do that let's continue out

play10:07

of this here just so we could get to the

play10:09

grid because I now want to demonstrate

play10:11

how I can modify the background if I

play10:14

wanted let's say to Green which looks

play10:17

horrible so let's go ahead and switch it

play10:20

back but it's nice to know that you have

play10:22

a little flexibility to toy around with

play10:23

it that looks slightly better but really

play10:26

we could just put it back to white but

play10:29

there's even a cool way to manipulate

play10:31

the page especially if you're using Edge

play10:34

so let's go ahead and do that now if you

play10:36

like to use Edge to debug you'll want to

play10:39

make sure that your launch. Json is

play10:42

configured properly to use Edge pointing

play10:44

to the correct Port once that's set you

play10:47

can choose run and debug and after you

play10:50

actually click the play button it should

play10:53

launch but the main reason I wanted to

play10:55

demonstrate this is because of this

play10:57

extra option here which was not in

play10:59

Chrome and this will give you the option

play11:02

to access the edge developer tools

play11:04

within vs code so when we click it what

play11:07

you'll need to do is install Microsoft

play11:10

Edge tools for vs code once that's done

play11:14

and you click it again now you have

play11:16

access to the same tools that you would

play11:18

in the edge browser so now let's go

play11:21

ahead and Shrink our window and make

play11:23

some space so we could see what we have

play11:26

access to and also from here we could

play11:29

actually modify some elements such as in

play11:33

this body here I can go ahead and choose

play11:37

whatever color I want that text to be

play11:39

whether it's red or green a darker green

play11:42

and it makes it a much nicer experience

play11:45

let's change it back to black over here

play11:47

for the background I can quickly modify

play11:50

whichever way that I want and again I'm

play11:53

fine with leaving it at a nice white

play11:56

background and so you can play around

play11:58

with this and see other other elements

play11:59

you might want to modify using the

play12:01

developer tools as you would in the

play12:03

browser but now you have the convenience

play12:05

of doing it within vs code which is

play12:08

excellent hope you enjoyed that I want

play12:11

to give a quick shout out to softar

play12:13

Jamal for his react app that I

play12:15

referenced from GitHub and you could

play12:18

find some more of his work in this link

play12:20

over here now we covered a lot but

play12:24

there's even more in the vs code docs on

play12:27

debugging at this link over here if you

play12:31

want me to make another video on other

play12:33

debugging topics like configuring

play12:36

launch. Json or anything else in the

play12:40

docs let's have at least 50 people

play12:42

mention in the comments part two and

play12:46

we'll make that happen if you got value

play12:48

out of this video definitely hit the

play12:51

like button and subscribe to help our

play12:53

Channel grow and I will see you in the

play12:56

next one

Rate This

5.0 / 5 (0 votes)

相关标签
VS CodeDebuggingJavaScriptC ProgrammingReactWeb DevelopmentDeveloper TipsCode DebugTech TutorialIDE
您是否需要英文摘要?