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
€96.03
List Price:  €134.44
You save:  €38.41
£78.97
List Price:  £110.57
You save:  £31.59
CA$143.92
List Price:  CA$201.50
You save:  CA$57.57
A$160.15
List Price:  A$224.22
You save:  A$64.06
S$135.90
List Price:  S$190.26
You save:  S$54.36
HK$776.95
List Price:  HK$1,087.76
You save:  HK$310.81
CHF 89.61
List Price:  CHF 125.46
You save:  CHF 35.85
NOK kr1,132.24
List Price:  NOK kr1,585.18
You save:  NOK kr452.94
DKK kr716.48
List Price:  DKK kr1,003.10
You save:  DKK kr286.62
NZ$176.72
List Price:  NZ$247.41
You save:  NZ$70.69
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,949.45
List Price:  ৳16,729.71
You save:  ৳4,780.25
₹8,504.71
List Price:  ₹11,906.93
You save:  ₹3,402.22
RM450.50
List Price:  RM630.72
You save:  RM180.22
₦155,597.43
List Price:  ₦217,842.63
You save:  ₦62,245.20
₨27,817.89
List Price:  ₨38,946.16
You save:  ₨11,128.27
฿3,454.30
List Price:  ฿4,836.16
You save:  ฿1,381.86
₺3,507.24
List Price:  ₺4,910.28
You save:  ₺1,403.03
B$630.94
List Price:  B$883.35
You save:  B$252.40
R1,822.81
List Price:  R2,552.01
You save:  R729.20
Лв187.70
List Price:  Лв262.78
You save:  Лв75.08
₩144,917.66
List Price:  ₩202,890.52
You save:  ₩57,972.86
₪361.94
List Price:  ₪506.74
You save:  ₪144.79
₱5,900.06
List Price:  ₱8,260.32
You save:  ₱2,360.26
¥15,702.92
List Price:  ¥21,984.72
You save:  ¥6,281.80
MX$2,038.30
List Price:  MX$2,853.70
You save:  MX$815.40
QR368.69
List Price:  QR516.19
You save:  QR147.49
P1,365.11
List Price:  P1,911.21
You save:  P546.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,089.47
List Price:  E£7,125.47
You save:  E£2,035.99
ብር12,635.69
List Price:  ብር17,690.47
You save:  ብር5,054.78
Kz91,513.08
List Price:  Kz128,121.98
You save:  Kz36,608.89
CLP$99,000
List Price:  CLP$138,603.96
You save:  CLP$39,603.96
CN¥729.57
List Price:  CN¥1,021.43
You save:  CN¥291.86
RD$6,083.32
List Price:  RD$8,516.89
You save:  RD$2,433.57
DA13,417.46
List Price:  DA18,784.98
You save:  DA5,367.52
FJ$231.78
List Price:  FJ$324.51
You save:  FJ$92.72
Q770.27
List Price:  Q1,078.41
You save:  Q308.14
GY$20,904.88
List Price:  GY$29,267.67
You save:  GY$8,362.78
ISK kr13,874.61
List Price:  ISK kr19,425.01
You save:  ISK kr5,550.40
DH998.53
List Price:  DH1,397.99
You save:  DH399.45
L1,842.29
List Price:  L2,579.28
You save:  L736.99
ден5,908.76
List Price:  ден8,272.50
You save:  ден2,363.74
MOP$800.37
List Price:  MOP$1,120.56
You save:  MOP$320.18
N$1,804.90
List Price:  N$2,526.94
You save:  N$722.03
C$3,679.90
List Price:  C$5,152.01
You save:  C$1,472.10
रु13,596.02
List Price:  रु19,034.98
You save:  रु5,438.95
S/373.62
List Price:  S/523.08
You save:  S/149.46
K405.04
List Price:  K567.08
You save:  K162.03
SAR375.67
List Price:  SAR525.96
You save:  SAR150.28
ZK2,767.31
List Price:  ZK3,874.34
You save:  ZK1,107.03
L477.70
List Price:  L668.80
You save:  L191.10
Kč2,412.77
List Price:  Kč3,377.98
You save:  Kč965.20
Ft39,808.32
List Price:  Ft55,733.24
You save:  Ft15,924.92
SEK kr1,103.71
List Price:  SEK kr1,545.23
You save:  SEK kr441.52
ARS$102,213.30
List Price:  ARS$143,102.71
You save:  ARS$40,889.41
Bs690.92
List Price:  Bs967.31
You save:  Bs276.39
COP$438,077.06
List Price:  COP$613,325.41
You save:  COP$175,248.35
₡50,326.04
List Price:  ₡70,458.47
You save:  ₡20,132.42
L2,538.30
List Price:  L3,553.73
You save:  L1,015.42
₲780,569.17
List Price:  ₲1,092,828.06
You save:  ₲312,258.89
$U4,458.42
List Price:  $U6,241.97
You save:  $U1,783.54
zł409.45
List Price:  zł573.24
You save:  zł163.79
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.