Using the Console to Debug

Modern JavaScript Debugging and Deploying
17 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.23
List Price:  €130.53
You save:  €37.29
£79.94
List Price:  £111.92
You save:  £31.98
CA$136.79
List Price:  CA$191.52
You save:  CA$54.72
A$153.20
List Price:  A$214.48
You save:  A$61.28
S$135.94
List Price:  S$190.32
You save:  S$54.38
HK$782.80
List Price:  HK$1,095.96
You save:  HK$313.15
CHF 91.30
List Price:  CHF 127.83
You save:  CHF 36.52
NOK kr1,094.90
List Price:  NOK kr1,532.91
You save:  NOK kr438
DKK kr695.39
List Price:  DKK kr973.58
You save:  DKK kr278.18
NZ$167.81
List Price:  NZ$234.95
You save:  NZ$67.13
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,958.03
List Price:  ৳15,341.69
You save:  ৳4,383.65
₹8,331.80
List Price:  ₹11,664.86
You save:  ₹3,333.05
RM477.70
List Price:  RM668.80
You save:  RM191.10
₦126,689.32
List Price:  ₦177,370.12
You save:  ₦50,680.80
₨27,798.13
List Price:  ₨38,918.49
You save:  ₨11,120.36
฿3,702.20
List Price:  ฿5,183.23
You save:  ฿1,481.03
₺3,249.97
List Price:  ₺4,550.10
You save:  ₺1,300.12
B$514.66
List Price:  B$720.55
You save:  B$205.88
R1,901.16
List Price:  R2,661.70
You save:  R760.54
Лв182.41
List Price:  Лв255.39
You save:  Лв72.97
₩137,421.64
List Price:  ₩192,395.79
You save:  ₩54,974.15
₪379.55
List Price:  ₪531.39
You save:  ₪151.83
₱5,778.57
List Price:  ₱8,090.23
You save:  ₱2,311.66
¥15,552.99
List Price:  ¥21,774.81
You save:  ¥6,221.82
MX$1,705.36
List Price:  MX$2,387.58
You save:  MX$682.21
QR364.16
List Price:  QR509.84
You save:  QR145.68
P1,384.82
List Price:  P1,938.81
You save:  P553.98
KSh13,448.65
List Price:  KSh18,828.65
You save:  KSh5,380
E£4,789.16
List Price:  E£6,705.01
You save:  E£1,915.85
ብር5,677.33
List Price:  ብር7,948.49
You save:  ብር2,271.15
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$95,258.47
List Price:  CLP$133,365.67
You save:  CLP$38,107.20
CN¥724.52
List Price:  CN¥1,014.36
You save:  CN¥289.84
RD$5,872.31
List Price:  RD$8,221.47
You save:  RD$2,349.16
DA13,431.87
List Price:  DA18,805.15
You save:  DA5,373.28
FJ$229.18
List Price:  FJ$320.86
You save:  FJ$91.68
Q776.12
List Price:  Q1,086.60
You save:  Q310.48
GY$20,901.55
List Price:  GY$29,263
You save:  GY$8,361.45
ISK kr13,994.60
List Price:  ISK kr19,593
You save:  ISK kr5,598.40
DH1,013.23
List Price:  DH1,418.56
You save:  DH405.33
L1,780.93
List Price:  L2,493.37
You save:  L712.44
ден5,743.61
List Price:  ден8,041.28
You save:  ден2,297.67
MOP$805.67
List Price:  MOP$1,127.97
You save:  MOP$322.30
N$1,914.68
List Price:  N$2,680.63
You save:  N$765.95
C$3,675.06
List Price:  C$5,145.23
You save:  C$1,470.17
रु13,310.19
List Price:  रु18,634.81
You save:  रु5,324.61
S/370.43
List Price:  S/518.62
You save:  S/148.18
K379.55
List Price:  K531.39
You save:  K151.83
SAR375.02
List Price:  SAR525.04
You save:  SAR150.02
ZK2,608.51
List Price:  ZK3,652.02
You save:  ZK1,043.50
L463.96
List Price:  L649.56
You save:  L185.60
Kč2,349.11
List Price:  Kč3,288.85
You save:  Kč939.73
Ft36,594.60
List Price:  Ft51,233.91
You save:  Ft14,639.30
SEK kr1,086.27
List Price:  SEK kr1,520.82
You save:  SEK kr434.55
ARS$87,315.45
List Price:  ARS$122,245.13
You save:  ARS$34,929.67
Bs689.99
List Price:  Bs966.01
You save:  Bs276.02
COP$389,137.18
List Price:  COP$544,807.62
You save:  COP$155,670.43
₡50,081.85
List Price:  ₡70,116.60
You save:  ₡20,034.74
L2,464.83
List Price:  L3,450.86
You save:  L986.03
₲741,742.01
List Price:  ₲1,038,468.49
You save:  ₲296,726.47
$U3,834.62
List Price:  $U5,368.62
You save:  $U1,534
zł402.27
List Price:  zł563.20
You save:  zł160.92
Already have an account? Log In

Transcript

In this final section, we're going to talk about debugging and deploying our JavaScript code. First, we will deal with the console and how we can use the console to help with debugging our code. Now, we've been using the console quite a bit already. But there are some additional things we can look at. Now the console is a part of the browser. So technically, each browser will have its own console API.

And therefore, the console could be different. However, the things we will be looking at are pretty consistent across browsers. But if you wanted to find out what the console API is for a particular browser, for example, this webpage right here reflects the console for Chrome You can simply google console API, Chrome console API, Safari console API Firefox, and you can find some resources that will give you information about the console. We will not be talking about every single command that's available. But we'll be looking at ones that I think are helpful. So let me review those first and then we will take a look at an actual debugging exercise.

So first off, we have console dot log that we've used multiple times. Now, most the time so far, when we've used console dot log, we've just passed in one parameter, a string or something like that. Well, separating parameters with commas allow you to pass in as many as you want. We'll take a look at that when we do the example. There are other ways to enter log messages in the console. info in Warren, for example, and it just formats it in a slightly different way.

Console dot air causes your console message to display as an error. And there can be some advantages to that, you can see what's called the call stack, which is basically the progression of functions that have been called before it hits this error. Console dot count, you can only pass in a single parameter, but it displays each time this is called on a separate line, and it gives it a number. So it's a great way to find out how many times a certain piece of code is executed. We've also looked at console dot Dir. In the past, that's been great.

If you have a node that you want to display the contents of you can display it with a dir And you're able to look at its contents easier. Finally, we have console time and console that time. And basically, this is used if you want to find out how long it takes for a particular piece of code to execute. So we'll take a look at that in just a minute as well. Now, I'm going to use our event reporter for just demoing some debugging tasks. Now I've added some errors within the event room Porter.

These errors are common. These are ones I encounter frequently myself when I'm doing JavaScript. In fact, one of the errors that I put into the event reporter was one I actually had when I was first writing it. The goal of this is just to show you the process. We can go through and using the console to debug our JavaScript code. If you write enough JavaScript, even if you write a very little bit of JavaScript, you're going to encounter bugs.

It's just the nature of writing code. And so you need to have a way to debug that code. Now for our event reporter, let me go ahead and refresh that. And I can see nothing is working, it's not working the way it's supposed to. So the first thing that I usually do when things are not working is I take a look at the console to see if there are any error messages. So let me open up the console for that purpose.

Sure enough, I have an error message. It says syntax error. We have a missing right prin. Now it's telling me what it thinks the error is or where it ran into a problem with continuing execution. That is not always the air, but it does give me a place to begin looking for the air Now one thing I can do is I can come over here to the line number. And it tells me where the area is what JavaScript file there is in, and also the line number, but I can click on that, and it will open it up.

It goes to this sources tab and opens up that JavaScript file and places me on the line where the error has occurred. Now that can be helpful. If I have very long files, however, I generally prefer to go to the actual JavaScript file and just refer to the line number. So I have an editor that displays the line number. I have the line numbers being displayed, and I can go that line number and see what the problem is. So it's telling me something about a parentheses right here.

But it looks like my parentheses are okay. So the error must be happening somewhere else. And so if I look through that, I can see that right here, I'm missing a concatenation operator. And so that's probably what is causing that error. So I correct that, save it. Go ahead and execute it again.

Oh, I've got the load event registered, but nothing else seems to be working. So that gives me a clue. It got as far as the load event, but it didn't get any farther. I can open up the console again and see if there's another error. I'm sure enough, there's another error. This one gives me a little more information.

It says add listener is not defined. Now also, I can open up an error message, and I can see what's called the call stack. This is the order of functions that were called before the air was received. So it did The init function, then it did toggle event listeners and an error occurred. So there's a good chance that error might be in toggle event listeners. And I also have the line number as a key as well.

So let me jump out to my code. Going down to toggle event listeners is telling me 69, specifically 69 add listeners is not defined. Well. If I look back up here, ah, the reason it's not defined is because I have an S. I misspelled function name. So I'll go ahead and save that. Jump back out.

Okay, things seem to be working if I check the console There's no errors there. Now let me do some testing of the additional functionality. Let's try to remove the event listener, I'm going to type it Ctrl S. That did not work. Let me check the console. And there's no error either. And so these can be the more difficult bugs to troubleshoot if something is not working, but you're not getting an error.

And this is where you can use the console commands to help you find that problem. I know I'm trying to remove event listeners. So the problem could be in several places. It could be that the key handler is not calling things correctly. It could be the Remove event listener is not being called at all, or there could be something with, that's a problem within remove event listener. So, to figure out what's going on, I'm going to add a console statement.

Right inside remove event listener so I can see if it's called. I'm going to do a string something like called. Now with this console dot log, I'm going to put a comma, I want to display some additional things with this particular event. First off, if the console dot log statement shows up, I can see that it's called but if I can see it is called I also want to check to see my listeners to see if it has the information it needs to be able to remove the listener so I'm going to display listening as well. And then I think I'll display listeners dot length to just quickly tell me how long it is how many arguments that have or how many items within the array. So this console dot log statement will help me do some debugging.

So I'm gonna go ahead and save, refresh. Type Ctrl S and open up my console. Alright, so here's my console dot log statement. First, it tells me the string so I know it was called. Then the second item I had passed in was the array, the listeners array. And also the third item was the length.

So I'm seeing all those items. I can see that six I should have six so that looks correct. I can then take a look at the array to see what items gotten in there. So there is no event a function event function event function. So it doesn't tip me off right away. Now, by the way, this was the bug that I had when I was first creating this.

And so I was trying to decipher Why am I remove event listeners wasn't working. So another thing I can do is I can come out and I can change this to warn, I mean, not warn, I'm sorry, change it to air, I could change it to warn as well. But I want to use console dot air. I'm going to save this. Refresh again, do Ctrl S, open the console. Now, something that I get with console dot air, like I said, I get the call stack.

So I can see. key down handler was called then toggle eventlistener was called and then remove event listener. So that is the order of functions that should be called. And so that looks correct. And so when everything looks like it's being passed in cracked. At that point, I figured, well, there must be something wrong here.

And so I start doing some more thinking about this, I probably looked up remove event listener. And that's where I determined the problem was when I looked up, remove event listener to remind myself how that particular method works. Now what I discovered when I was looking at console dot log is I had the event first, then the function event function event function. So I was then popping off One of those off the array, and then another one. So then I thought, well, I may not have those in the correct order. So a way to find that out would be quick console dot log, listeners dot pop.

Ups whoops, and see what I got. This obviously will mess up my code. Because as soon as it pops it off, it's not going to be in the array anymore to be used down here, but it will allow me to see what the first one is that's popped off. So I save that refresh Ctrl S and open the console. So it looks like the first thing popped off is an is a function. And that quickly tells me Oh, I've got things backwards, the event should be the first thing, the function should be the second thing.

So now I know how to correct the problem. So I removed my console statements and then up where I'm adding the data to the listener array, I just need to switch them around. There's three places where I do this. So I need to make that change in three different places. And then for the last one Oh, I missed one right here. Now I save it.

Refresh it. Ctrl S. And now it's working. So a little bit of the process of how we can go through debugging. Now, let me just quickly show you some of the other console statements. For example, what if I did a console count inside my mouse move handler that would show me how many times that's called obviously with this if statement, I'm restricting how many times it prints out. But I could see how many times it's called using console count.

Save it. Refresh. Now Smoove. Looks like I've recorded six in the event reporter. If I open the console, I have a ton of them 188. That's how many times it's coming across, but I restrict how many times it actually prints out.

Okay. One more A console statement to show you is time and time. And that can be used to determine how long something takes. So let's put a console dot time statement up here at the top of this JavaScript file. And the label I'm going to use is report. That's basically all you pass in with console dot time is simply a label.

And then, when you want it to print out how long the process has taken, you put console dot time in with the same label. So I want to see how long it takes from when the JavaScript page is loaded and tell the init function is called. And then the entire init function executes. So I'm going to console dot time end and the same label reporter. Let's see how long That takes, save it. Refresh.

Let's open up the console. I still have the mouse move console count statements showing up. But I can see that it took 65.295 milliseconds from when the JavaScript file loaded until the load event fired off the init and the entire init function executed. So that can be helpful in determining how long certain code takes to execute as well. So those are a couple additional statements available in the console that you can use for debugging purposes. Let's move on to the next topic and look at the debugger.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.