Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Setting breakpoints on specific lines may be the most common ones you'll set.
Covered in this Video
- Line breakpoint
- Conditional line breakpoint
Equality vs. Itentity ( ==
vs. ===
)
While ==
(equality operator) and ===
(identity operator) might seem to do basically the same thing in JavaScript, many developers prefer using ===
to ==
. The reason is that in certain cases, predicting how ==
will behave can be counterintuitive. That's because the interpreter will make assumptions about how to treat the values you place on either side of ==
.
For example, consider
12 == '12' // true
Here, the interpreter will convert the string on the right to a number before comparing the two values and return true as a result. It's kind of saying, "I think you meant to give me a number for this second value. That's ok, I'll just change it for you."
Leaving ambiguity up to the interpreter to decide for you can lead to trouble in ways that are hard to predict. Moreover, bugs having to do with these assumptions can be hard to find!
That's why developers often prefer ===
for comparing values.
12 === '12' // false
12 === 12 // true
It helps them write code that's easy to understand, not only at the time of writing, but later, when reading back over the code.
Here's an MDN article on this topic for a little more depth.
-
0:00
There's another bug in this program as well.
-
0:03
If I check the Hide checkbox, all the guests who haven't
-
0:07
RSVPd should disappear from the page, but they don't disappear.
-
0:13
Instead, they become highlighted as if they were confirmed.
-
0:17
No further changes happen when I check or uncheck this box.
-
0:23
Let's inspect one of these items to see what may be happening.
-
0:27
I'll refresh and inspect Tammy's list item in the elements tab.
-
0:38
Now I will check the hide box again.
-
0:42
And you can see there's a problem here.
-
0:44
All of the guests who were not marked responded have been
-
0:49
added the class of responded.
-
0:51
And they also should be getting an inline display property of
-
0:56
none to make them hide from the page.
-
0:59
But that's not happening, which you can see here.
-
1:02
And you can also confirm that by looking down at its display property right here.
-
1:08
It's still list-item.
-
1:10
Setting an event listener breakpoint worked really well before,
-
1:14
so let's do it again here.
-
1:16
I'll go to the sources tab.
-
1:19
Set the breakpoint.
-
1:23
And it looks like it's already set from before, so
-
1:26
I'll leave the change event checked.
-
1:29
Now, I'll refresh and check the hide box.
-
1:34
The debugger's paused where the listener begins on line 17.
-
1:39
Before we start stepping through the program,
-
1:42
I'm immediately curious about what's happening in this for loop down here.
-
1:47
And that's because that's where the list items are being manipulated in our code.
-
1:53
We can jump to a specific line of code by setting a breakpoint.
-
1:58
While we previously set breakpoints on browser events,
-
2:01
this time we'll set one on a line.
-
2:04
To set a breakpoint click the line number where you want to break at.
-
2:09
Let's put our breakpoint on the line right after line 22 where
-
2:14
the list item is set up in the loop.
-
2:16
So line 23.
-
2:18
We can do this by clicking on 23 and to remove a breakpoint,
-
2:22
I'll just click it again.
-
2:25
And I'll put it back.
-
2:26
Now any time the JavaScript interpreter reaches this point,
-
2:30
it will pause right here.
-
2:31
You don't need to step through each line of code from 17 down to 23 either.
-
2:37
The play button right here and pauses the interpreter letting it run until
-
2:41
it reaches a breakpoint or the end of the program.
-
2:45
I'll press the play button now, see how it jump down there?
-
2:50
Now we're inside the first time through the loop.
-
2:53
You can tell because i is set to 0.
-
2:58
Also, if we hover over the li element in the scope window,
-
3:03
you can see the first name highlighted on the page.
-
3:06
Because our bug only affects unchecked guests, though, we might want to jump
-
3:11
ahead until we get to the fourth guest to see what is happening.
-
3:15
If I press play again, the loop will cycle around and land on the breakpoint.
-
3:23
Now i: 1 and li points to the second list item.
-
3:29
I'll press play two more times, and now we've landed on our unchecked guest.
-
3:36
Let's say we had a list of 200 guests though and
-
3:40
our bug only showed up at number 175.
-
3:43
That would mean we'd need to press the play button 175 times.
-
3:49
Fortunately, dev tools also lets us set a breakpoint
-
3:52
that will only pause if a certain condition is met.
-
3:57
Let's refresh this page and I'll show you how it works.
-
4:01
I'll go ahead and check the box to pause the execution at the top of our handler.
-
4:07
I'll click this breakpoint off.
-
4:10
Then I'll right-click line 23 and choose Add conditional breakpoint.
-
4:16
I can enter any condition here like
-
4:21
lI.classname is not equal to responded.
-
4:28
Now when I hit play, the loop won't stop until we get to the first
-
4:32
lI element without a responded class.
-
4:35
I'll hit enter to set our breakpoint, and now I'll hit play.
-
4:41
And we can verify it worked by hovering over the value and
-
4:45
you can see the fourth li is highlighted.
-
4:48
Now let me just show you one thing.
-
4:51
If I edit the breakpoint, we could've added any expression in here.
-
4:56
The same thing would've happened if we'd used i is strictly equal to 3,
-
5:01
for example.
-
5:03
If possible, though, it's better to pick an expression that gets you directly to
-
5:07
the situation you're diagnosing.
-
5:11
In this case, it's when an element is missing a certain class name.
-
5:17
I'll step forward now and
-
5:19
we'll expect to jump into the else clause of this if statement.
-
5:23
That's because the current list item's class name is not responded.
-
5:29
But the first branch is executing.
-
5:32
Moreover, the style of the element has changed and over in scope,
-
5:37
I can see that the class of responded has been added.
-
5:40
Can you spot the bug?
-
5:44
The single equal sign is an assignment operator.
-
5:48
It assigns the class to that element.
-
5:51
But it should be testing the condition using an equality operator.
-
5:55
So this should be three equal signs and not just one.
-
5:59
If you're wondering why three and not two, see the teacher's notes for
-
6:02
a more detailed discussion of the two operators.
-
6:05
I'll refresh, switch over to my editor.
-
6:09
And on line 23, I'll add the two more equals signs and save and
-
6:14
I'll switch back.
-
6:18
And now I'll disable all my breakpoints so
-
6:21
I can see if it's fixed or not and I'll check the hide box.
-
6:28
I need to refresh.
-
6:30
Now I'll check the hide box.
-
6:33
And you can see it works.
-
6:37
Let's review what we learned in this video.
-
6:40
We looked at two kinds of break points you can set on lines of your program.
-
6:45
A line breakpoint, and a conditional line breakpoint.
-
6:49
You'll probably use these a lot in your debugging process because they're so
-
6:53
versatile.
-
6:54
In the next video I'll show you how to debug functions.
You need to sign up for Treehouse in order to download course files.
Sign up