Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Debugging with VS Code
📖 Scenario: You are building a simple Node.js program that calculates the total price of items in a shopping cart. Sometimes the total is incorrect, so you want to learn how to debug your code using Visual Studio Code.
🎯 Goal: Learn how to set up a Node.js project and use VS Code's debugger to find and fix errors in your code step-by-step.
📋 What You'll Learn
Create a list of items with prices
Add a variable to hold the total price
Use a loop to sum the prices
Add a breakpoint and debug the code in VS Code
💡 Why This Matters
🌍 Real World
Debugging is a key skill for fixing errors in real software projects. Using VS Code's debugger helps you understand what your code is doing step-by-step.
💼 Career
Many software development jobs require debugging skills. Knowing how to use VS Code's debugger makes you more efficient and confident in finding and fixing bugs.
Progress0 / 4 steps
1
Create the items list
Create a constant array called items with these exact objects: { name: 'apple', price: 1.2 }, { name: 'banana', price: 0.8 }, and { name: 'orange', price: 1.5 }.
Node.js
Hint
Use const items = [ ... ] and include the exact objects inside the array.
2
Add a total variable
Add a variable called total and set it to 0 to hold the sum of prices.
Node.js
Hint
Use let total = 0; to create the variable.
3
Sum the prices with a loop
Use a for loop with variable item to go through items and add item.price to total inside the loop.
Node.js
Hint
Use for (const item of items) { total += item.price; } to sum prices.
4
Add a breakpoint and debug
Add a debugger; statement inside the for loop after adding item.price to total. This will let you pause and inspect variables in VS Code's debugger.
Node.js
Hint
Insert debugger; inside the loop to pause execution in VS Code.
Practice
(1/5)
1. What is the main purpose of setting a breakpoint in VS Code when debugging a Node.js application?
easy
A. To speed up the execution of the program
B. To automatically fix syntax errors in the code
C. To convert JavaScript code into machine code
D. To pause the program at a specific line to inspect variables and program flow
Solution
Step 1: Understand what a breakpoint does
A breakpoint pauses the program execution at a chosen line so you can check what is happening.
Step 2: Identify the purpose in debugging
Pausing lets you inspect variables and see how the program flows step-by-step.
Final Answer:
To pause the program at a specific line to inspect variables and program flow -> Option D
Quick Check:
Breakpoint = Pause and inspect [OK]
Hint: Breakpoints pause code to check variables and flow [OK]
Common Mistakes:
Thinking breakpoints fix errors automatically
Believing breakpoints speed up code
Confusing breakpoints with code compilation
2. Which of the following is the correct way to start debugging a Node.js app in VS Code using the launch.json file?
easy
A. { "type": "node", "request": "run", "program": "${workspaceFolder}/app.js" }
B. { "type": "node", "request": "launch", "program": "${workspaceFolder}/app.js" }
C. { "type": "node", "request": "compile", "program": "app.js" }
D. { "type": "python", "request": "launch", "program": "app.js" }
Solution
Step 1: Check the correct debug type for Node.js
The type must be "node" to debug Node.js applications.
Step 2: Verify the request and program fields
"request" should be "launch" to start debugging, and "program" points to the main file with workspace variable.
Hint: Verify launch.json exists and is correct to enable breakpoints [OK]
Common Mistakes:
Blaming Node.js version without checking config
Assuming breakpoints need browser debugging
Not saving files but that usually doesn't stop breakpoints
5. You want to debug a Node.js app that uses environment variables. How can you configure launch.json to pass an environment variable named API_KEY with value 12345 to your app during debugging?