0
0
Node.jsframework~30 mins

Debugging with VS Code in Node.js - Hands-On Comparison

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Insert debugger; inside the loop to pause execution in VS Code.