Pause your own program in VS Code on a line you choose: set a breakpoint, run Node in a JavaScript Debug Terminal, read the boxes in VARIABLES and the pile in CALL STACK, and step over, into and out of calls to find a bug that prints no error.
TypeScript
Fundamentals
The debugger: stepping through a function. Read this lesson at .
Tested with TypeScript 7.x · Node.js LTS
In lesson 5.8 a program crashed, and Node told you where: a stack
trace, read from the top, down to the call that handed over the
bad value. The bug in this lesson is quieter. Your end-of-day
summary prints 2 of 3 done (150%). Nothing crashes, TypeScript
has no complaint, and there is no trace to read. Just a number
that cannot be right.
Think of the replay of a football match. When the referee is not sure
about a goal, nobody argues from the final score: they pause the
replay at the moment that matters and look at where every player
stands, then play it on, one moment at a time. A debugger does
that for a program. By the end of this lesson you will pause your own
program on a line you choose, read every box and the whole call stack
in VS Code (Visual Studio Code, your editor), move through it one line
or one call at a time, and find a bug that prints no error at all.
A bug with no error message
Here is day.ts from lesson 5.8, with one small change. Run it:
Two tasks out of three should be 67%. TypeScript checked every
type, and every type is right: a number went wherever a number
was wanted. Node ran every line without trouble. The program does
exactly what it says; it just says the wrong thing, somewhere.
You could hunt for it with console.log, as lesson 5.8 did with
console.trace: add a line that prints a box, run, read, add
another, run again, and delete them all at the end. That works,
but every new question costs an edit and a run, and you have to
guess in advance which boxes you will want to see.
A debugger saves you those edits. It pauses your program while it
runs, on a line you pick, and lets you look at every box and at
the call stack right there. Then the program carries on, one line
at a time if you like, only when you say so. VS Code has one built
in, and it works with the Node you already use.
Pause on a line: a breakpoint
You tell the debugger where to stop Node with a breakpoint: a mark on a
line that means "stop just before you run this". Node runs at full
speed until it reaches that line, then waits. Here is how to set
one and run day.ts so that it stops.
1
Make day.ts in your project
In VS Code, open ~/projects/taskkit, make a file called day.ts
next to index.ts, and paste in the code above. Save it.
2
Set a breakpoint on line 2
Move the pointer just left of the line number 2. That strip is the
gutter, the margin beside the line numbers, and a faint red dot
appears in it. Click, and the dot turns solid red: a breakpoint.
Clicking it again removes it. You can also put the cursor on
line 2 and press:
F9
On many laptops you hold fn for an F key.
3
Open a JavaScript Debug Terminal
An ordinary terminal runs Node on its own. A JavaScript Debug
Terminal is a VS Code terminal that hands every Node program you
start in it to the debugger. Open the Command Palette, the box
where you run any VS Code command by typing its name (on a Mac,
lesson 1.2 used it to install code):
⌘ Cmd+⇧ Shift+P
Ctrl+Shift+P
Ctrl+Shift+P
Type debug terminal and choose Debug: JavaScript Debug
Terminal. A new terminal opens in your project folder, next to
any you already have. You can also pick JavaScript Debug
Terminal from the small arrow beside + at the top of the
terminal panel.
4
Run day.ts in it
bash
node day.ts
Outputtext
Debugger attached.
Node says the debugger has joined it, starts running, and stops
before line 2. Nothing else is printed yet: console.log on
line 10 has not been reached.
What VS Code shows while Node waits
When Node stops, VS Code switches to the Run and Debug view in
its side bar and brings the paused line into sight. Line 2 gets a
yellow arrow in the gutter and a band across it, the same line
that is lit here:
Read that band carefully: it marks the line Node will run next.
Line 2 has not run yet. Everything above it that was needed to get
here has. Of the panels in the side bar, two matter here: VARIABLES
at the top and CALL STACK below it:
VARIABLES
Local: percentDone part: 3 whole: 2
VARIABLES
Does: Shows the boxes on the card picked in CALL STACK
Does: Shows the pile of calls under way, the newest on top
Paused on line 2: the boxes of the call, and the pile under it
VARIABLES lists the boxes of the call that is paused. Local: percentDone means "the boxes on percentDone's own card", and
there are its two parameters, part and whole, with the values
this call was given. You will also see a this row and folded
Closure and Global sections; leave them closed for now.
CALL STACK is the pile of cards from lesson 5.8, each card one
call's frame, drawn by VS Code, the newest card on top: percentDone
is running, scoreLine waits on line 5, endOfDay waits on line 10.
After each name come the file, the line and the column the card waits
on. <anonymous> is the file itself, the card lesson 5.8 called
global and Node's trace called Object.<anonymous>. VS Code may
list a few of Node's own cards under it, in grey; they are not your
code, and you can leave them alone. VS Code may also write global.
in front of your functions' names: global.percentDone is just
percentDone, declared in the file itself.
You can also point at a name in the editor while Node waits. Hover
over part on line 2, and VS Code shows its value, 3, beside the
pointer.
Now look at the numbers. part is 3 and whole is 2, and 3 out of
2 is 150%. percentDone is about to do its sum correctly, with the
wrong numbers. The bug is not on line 2. The question is where 3
and 2 came from.
Down the pile, to where the numbers came from
Every card in CALL STACK can be clicked. Clicking a card does not
move Node anywhere; it only changes what you are looking at. The
editor jumps to the line that card waits on, and VARIABLES shows
that card's boxes instead. It is lesson 5.8's rule for a stack
trace, read downward from the top, with every card's boxes open.
Press Next layer to walk down the pile, or let it play:
percentDone · day.ts 2:3
part 3, whole 2: upside down, so 3 of 2 is 150%
scoreLine · day.ts 5:19
done 2, all 3 are right, and line 5 swaps them
endOfDay · day.ts 10:15
all 3, from two tasks done and one left: right
<anonymous> · day.ts 12:1
The file itself, at the bottom: where it all started
Click each card in CALL STACK and read its boxes
All 4 layers shown.Layer 1 of 4: percentDone · day.ts 2:3Layer 2 of 4: scoreLine · day.ts 5:19Layer 3 of 4: endOfDay · day.ts 10:15Layer 4 of 4: <anonymous> · day.ts 12:1
All 4 layers shown.
One card down, in scoreLine, the numbers are the right way round:
done is 2 and all is 3. scoreLine waits on line 5, and that line
hands the numbers to percentDone: percentDone(all, done). The
parameters of percentDone are part, then whole, so the 3 lands
in part and the 2 in whole. That is the bug: two arguments in the
wrong order, which TypeScript cannot catch, because both are numbers.
One line at a time: the step buttons
Finding the line is half the job. Before you fix it, watch the swap
happen, a step at a time. While Node is paused, a small toolbar sits
at the top of the editor. These are its buttons, and the keys that
do the same:
Button
Key
What it does
Continue
F5
Run on, to the next breakpoint or the end
Step Over
F10
Run the yellow line whole, calls and all
Step Into
F11
Go into the call on the yellow line
Step Out
Shift+F11
Finish this call, pause in its caller
Stop
Shift+F5
End the program here
The keys are the same on every system, and on a Mac F11 may show the
desktop instead. The buttons work everywhere, so click them while you
learn.
Click the dot on line 2 to remove it and set a breakpoint on line 5
instead. Press Continue so this run finishes, then run
node day.ts again in the same debug terminal. Node pauses on line 5
this time, in scoreLine, before it calls percentDone. From here,
each of the three step buttons leaves you somewhere different. Press
Next step to walk through it, or let it play, and switch between
the three buttons:
VSCode
You
Node
VS Code: paused on line 5pausedonline5pausedonline5
You: Step OverStepOverStepOver
Node: runs all of line 5runsallofline5runsallofline5
VS Code: line 6 · percent: 150line6·percent:150line6·percent:150
Time
Node is paused in scoreLine, before line 5 runs.
You press Step Over, or F10.
Node runs line 5 whole, the call of percentDone included,
without pausing inside it.
Paused again on line 6, still in scoreLine. Line 5 has run,
so percent holds its answer.
Where each button leaves you, from a pause on line 5
All 4 steps shown.Step 1 of 4: VS Code: paused on line 5Step 2 of 4: You: Step OverStep 3 of 4: Node: runs all of line 5Step 4 of 4: VS Code: line 6 · percent: 150
All 4 steps shown.
VSCode
You
Node
VS Code: paused on line 5pausedonline5pausedonline5
Node starts the call on line 5 and stops at the first line of
percentDone.
Paused on line 2, inside percentDone: a new card on top of
CALL STACK, with its own boxes.
Where each button leaves you, from a pause on line 5
All 4 steps shown.Step 1 of 4: VS Code: paused on line 5Step 2 of 4: You: Step IntoStep 3 of 4: Node: enters percentDoneStep 4 of 4: VS Code: line 2 · part: 3
All 4 steps shown.
VSCode
You
Node
VS Code: paused on line 5pausedonline5pausedonline5
VS Code: line 10 · in endOfDayline10·inendOfDayline10·inendOfDay
Time
Node is paused in scoreLine, before line 5 runs.
You press Step Out, or Shift+F11.
Node runs the rest of scoreLine and hands its answer back to
the line that called it.
Paused on line 10, in endOfDay. scoreLine's card has come
off CALL STACK.
Where each button leaves you, from a pause on line 5
All 4 steps shown.Step 1 of 4: VS Code: paused on line 5Step 2 of 4: You: Step OutStep 3 of 4: Node: finishes scoreLineStep 4 of 4: VS Code: line 10 · in endOfDay
All 4 steps shown.
Step Over is for a call you trust: it runs the whole line, the
call inside it included, and pauses on the next line of the same
function. Step Into is for a call you suspect: it follows the
call, so a new card goes on top of CALL STACK and you pause on the
first line of the function. Step Out is for when you have seen
enough of a function: Node finishes it, its card comes off the
pile, and you pause back in the function that called it.
Press Step Into on line 5 now and watch the swap happen: the
card for percentDone appears, and VARIABLES shows part: 3,
whole: 2, straight from percentDone(all, done). Step Into on a
line with no call of yours on it works like Step Over. It also
never takes you into Node's own code, such as console.log: those
cards are skipped, and you pause on the next line of yours.
Press Continue to let the program finish; it prints
2 of 3 done (150%) in the debug terminal, then
Waiting for the debugger to disconnect..., Node's note that it is
handing the program back to you. Then fix line 5, so the
numbers go in the order percentDone asks for:
The breakpoint is the part you can see, so it is the part people
check. The terminal is the part that decides whether it works.
Common mistake
Running the program in an ordinary terminal
You set a red dot on line 2 of the first day.ts, the one with the
bug, open a terminal with the usual keys from lesson 1.2, and type
node day.ts. The program runs to its end and
prints its answer, and the red dot is ignored.
A breakpoint is only a note to the debugger. An ordinary terminal
starts Node on its own, with no debugger to read the note.
2 of 3 done (150%)
The fix
Run the program in a JavaScript Debug Terminal: Command
Palette, Debug: JavaScript Debug Terminal, then the same
command.
bash
node day.ts
Outputtext
Debugger attached.
The same command, in each kind of terminal:
% node day.ts
2 of 3 done (150%)
%
An ordinary terminal
Never: It never stops at a breakpoint
% node day.ts
Debugger attached.
JavaScript Debug Terminal
Does: Node waits on line 2, and VS Code shows its boxes
The same command: only one terminal hands Node to VS Code
Does: Node waits on line 2, and VS Code shows its boxes
The same command: only one terminal hands Node to VS Code
$ node day.ts
2 of 3 done (150%)
$
An ordinary terminal
Never: It never stops at a breakpoint
$ node day.ts
Debugger attached.
JavaScript Debug Terminal
Does: Node waits on line 2, and VS Code shows its boxes
The same command: only one terminal hands Node to VS Code
Debugger attached. is the sign to look for: no such line, no
pause. If that line is there and Node still does not stop, look at
the dot itself. A breakpoint on an empty line or a lone } moves to
the nearest line that runs. A hollow grey circle means the debugger
did not place it at all, for example because the file you ran is not
the file with the dot.
This day.ts has a new swap. It prints 3 of 2 done (150%). Fix
it so it prints 2 of 3 done (67%), without changing
percentDone or scoreLine.
Loading the editor…
Exercise · Fix the error
Put the debugging steps in order
This program prints the steps that pause day.ts on line 2 and
find the swap, but in an order that would not work. Change the
order of the lines, without changing any line itself, so the steps
work from top to bottom.
Loading the editor…
Recap
Recap · 3 questions
Check yourself
Line 5 is marked in yellow. Has it run yet?
Check yourself
The yellow line calls a function you suspect. Which button?
Check yourself
A solid red dot is on line 2, yet node day.ts finishes. Why?
0 of 0 answered.
A cheat sheet to keep
Cheat sheet
The debugger: stepping through a function
What things are
Word
What it means
Debugger
A tool that pauses a running program for you to look
Breakpoint
A red dot: stop just before this line runs
Gutter
The margin left of the line numbers
JavaScript Debug Terminal
A terminal that hands Node to the debugger
Yellow line
The line Node runs next; it has not run yet
VARIABLES
The boxes on the card picked in CALL STACK
CALL STACK
The pile of calls under way, the newest on top
<anonymous>
The file itself, the bottom card
Buttons and keys
Press
To
F9
Set or remove a breakpoint on this line
Continue, F5
Run on to the next breakpoint or the end
Step Over, F10
Run the yellow line whole, calls and all
Step Into, F11
Pause on the first line of the call
Step Out, Shift+F11
Finish this call, pause in its caller
Stop, Shift+F5
End the program here
Cmd/Ctrl+Shift+P
Command Palette: run a command by name
Rules
Rule
What it means
Debug terminal
No Debugger attached., no pause
Not run yet
A box the yellow line makes shows undefined
Click a card
See its boxes; Node does not move
Read down the pile
Right boxes below wrong ones: the bug is between
Into or Over
Into a call you suspect, over one you trust
Hollow grey dot
Not placed: is this the file you ran?
Types are not meaning
Two numbers swapped still type-check
Where to go next
You can now pause your own program on a line, read every box on
every card of its call stack, and move through it a line or a call
at a time. You found a bug that printed no error, by reading down
the pile to the place where right numbers turned into wrong ones.
Next is Checking your work: your first test with node:test.
The debugger finds a bug once, while you watch. There you write a
test that calls scoreLine(2, 3) and checks for 67% every time
you run it, so this bug can never come back without you hearing
about it.
Before you move on, pause TaskKit. In ~/projects/taskkit, set a
breakpoint on the return line inside percentFull in index.ts,
open a JavaScript Debug Terminal, and run node index.ts add a b c.
Read added (3) and limit (5) in VARIABLES, then the pile in CALL
STACK: percentFull on top, printSummary under it, then the file
itself. Press Step Out twice and watch the cards come off, then
Continue, and remove the dot.