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
€94.79
List Price:  €132.71
You save:  €37.92
£78.97
List Price:  £110.56
You save:  £31.59
CA$139.89
List Price:  CA$195.85
You save:  CA$55.96
A$153.73
List Price:  A$215.23
You save:  A$61.50
S$134.32
List Price:  S$188.05
You save:  S$53.73
HK$778.26
List Price:  HK$1,089.59
You save:  HK$311.33
CHF 88.57
List Price:  CHF 124
You save:  CHF 35.43
NOK kr1,105.08
List Price:  NOK kr1,547.16
You save:  NOK kr442.07
DKK kr706.99
List Price:  DKK kr989.82
You save:  DKK kr282.82
NZ$170.06
List Price:  NZ$238.10
You save:  NZ$68.03
د.إ367.26
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,906.24
List Price:  ৳16,669.22
You save:  ৳4,762.97
₹8,436.24
List Price:  ₹11,811.08
You save:  ₹3,374.83
RM447.10
List Price:  RM625.96
You save:  RM178.86
₦167,464.25
List Price:  ₦234,456.65
You save:  ₦66,992.40
₨27,687.73
List Price:  ₨38,763.93
You save:  ₨11,076.20
฿3,470.35
List Price:  ฿4,858.63
You save:  ฿1,388.28
₺3,445.98
List Price:  ₺4,824.51
You save:  ₺1,378.53
B$578.68
List Price:  B$810.17
You save:  B$231.49
R1,810.53
List Price:  R2,534.81
You save:  R724.28
Лв185.34
List Price:  Лв259.49
You save:  Лв74.14
₩139,691.42
List Price:  ₩195,573.58
You save:  ₩55,882.15
₪373.67
List Price:  ₪523.15
You save:  ₪149.48
₱5,901.36
List Price:  ₱8,262.14
You save:  ₱2,360.78
¥15,574.86
List Price:  ¥21,805.43
You save:  ¥6,230.57
MX$2,020.46
List Price:  MX$2,828.72
You save:  MX$808.26
QR363.35
List Price:  QR508.71
You save:  QR145.35
P1,355.53
List Price:  P1,897.79
You save:  P542.26
KSh12,948.70
List Price:  KSh18,128.70
You save:  KSh5,180
E£4,969.50
List Price:  E£6,957.50
You save:  E£1,988
ብር12,262.61
List Price:  ብር17,168.15
You save:  ብር4,905.53
Kz91,256.94
List Price:  Kz127,763.36
You save:  Kz36,506.42
CLP$97,119.28
List Price:  CLP$135,970.88
You save:  CLP$38,851.60
CN¥724.58
List Price:  CN¥1,014.45
You save:  CN¥289.86
RD$6,000.34
List Price:  RD$8,400.72
You save:  RD$2,400.37
DA13,341.73
List Price:  DA18,678.95
You save:  DA5,337.22
FJ$227.04
List Price:  FJ$317.87
You save:  FJ$90.82
Q769.16
List Price:  Q1,076.85
You save:  Q307.69
GY$20,834.51
List Price:  GY$29,169.15
You save:  GY$8,334.63
ISK kr13,772.62
List Price:  ISK kr19,282.22
You save:  ISK kr5,509.60
DH995.44
List Price:  DH1,393.66
You save:  DH398.21
L1,819.82
List Price:  L2,547.83
You save:  L728
ден5,825.40
List Price:  ден8,155.79
You save:  ден2,330.39
MOP$798.74
List Price:  MOP$1,118.27
You save:  MOP$319.52
N$1,801.35
List Price:  N$2,521.97
You save:  N$720.61
C$3,666.61
List Price:  C$5,133.40
You save:  C$1,466.79
रु13,462.23
List Price:  रु18,847.67
You save:  रु5,385.43
S/378.10
List Price:  S/529.35
You save:  S/151.25
K400.90
List Price:  K561.28
You save:  K160.37
SAR375.38
List Price:  SAR525.55
You save:  SAR150.16
ZK2,752.36
List Price:  ZK3,853.42
You save:  ZK1,101.05
L471.69
List Price:  L660.38
You save:  L188.69
Kč2,395.79
List Price:  Kč3,354.20
You save:  Kč958.41
Ft38,840.72
List Price:  Ft54,378.57
You save:  Ft15,537.84
SEK kr1,099.74
List Price:  SEK kr1,539.68
You save:  SEK kr439.94
ARS$100,165.56
List Price:  ARS$140,235.79
You save:  ARS$40,070.23
Bs690.96
List Price:  Bs967.37
You save:  Bs276.41
COP$440,690.82
List Price:  COP$616,984.78
You save:  COP$176,293.95
₡50,645.88
List Price:  ₡70,906.26
You save:  ₡20,260.37
L2,517.41
List Price:  L3,524.47
You save:  L1,007.06
₲775,966.15
List Price:  ₲1,086,383.66
You save:  ₲310,417.50
$U4,274.58
List Price:  $U5,984.58
You save:  $U1,710
zł411.21
List Price:  zł575.71
You save:  zł164.50
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.