Debugging Your Code

JavaScript for Newbies Advanced Topics
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$39.99
List Price:  $79.99
You save:  $40
€38.34
List Price:  €76.69
You save:  €38.35
£31.81
List Price:  £63.63
You save:  £31.81
CA$57.41
List Price:  CA$114.84
You save:  CA$57.43
A$63.97
List Price:  A$127.96
You save:  A$63.98
S$54.21
List Price:  S$108.43
You save:  S$54.22
HK$310.92
List Price:  HK$621.91
You save:  HK$310.99
CHF 35.74
List Price:  CHF 71.50
You save:  CHF 35.75
NOK kr452.69
List Price:  NOK kr905.49
You save:  NOK kr452.80
DKK kr285.99
List Price:  DKK kr572.05
You save:  DKK kr286.06
NZ$70.70
List Price:  NZ$141.42
You save:  NZ$70.72
د.إ146.88
List Price:  د.إ293.80
You save:  د.إ146.92
৳4,759.62
List Price:  ৳9,520.44
You save:  ৳4,760.81
₹3,397.10
List Price:  ₹6,795.05
You save:  ₹3,397.95
RM180.27
List Price:  RM360.59
You save:  RM180.32
₦61,792.91
List Price:  ₦123,601.28
You save:  ₦61,808.36
₨11,086.54
List Price:  ₨22,175.86
You save:  ₨11,089.31
฿1,367.71
List Price:  ฿2,735.76
You save:  ฿1,368.05
₺1,402.52
List Price:  ₺2,805.39
You save:  ₺1,402.87
B$243.37
List Price:  B$486.81
You save:  B$243.44
R732.54
List Price:  R1,465.27
You save:  R732.72
Лв74.93
List Price:  Лв149.89
You save:  Лв74.95
₩57,842.33
List Price:  ₩115,699.13
You save:  ₩57,856.80
₪145.51
List Price:  ₪291.06
You save:  ₪145.55
₱2,352.81
List Price:  ₱4,706.21
You save:  ₱2,353.40
¥6,256.23
List Price:  ¥12,514.03
You save:  ¥6,257.80
MX$803.05
List Price:  MX$1,606.30
You save:  MX$803.25
QR145.19
List Price:  QR290.43
You save:  QR145.23
P550.51
List Price:  P1,101.16
You save:  P550.64
KSh5,142.14
List Price:  KSh10,285.57
You save:  KSh5,143.43
E£2,034.81
List Price:  E£4,070.14
You save:  E£2,035.32
ብር4,965.06
List Price:  ብር9,931.37
You save:  ብር4,966.30
Kz36,710.82
List Price:  Kz73,430.82
You save:  Kz36,720
CLP$39,506.29
List Price:  CLP$79,022.46
You save:  CLP$39,516.17
CN¥291.78
List Price:  CN¥583.63
You save:  CN¥291.85
RD$2,425.42
List Price:  RD$4,851.45
You save:  RD$2,426.03
DA5,393.73
List Price:  DA10,788.81
You save:  DA5,395.08
FJ$92.63
List Price:  FJ$185.29
You save:  FJ$92.66
Q306.89
List Price:  Q613.86
You save:  Q306.97
GY$8,332.99
List Price:  GY$16,668.06
You save:  GY$8,335.07
ISK kr5,538.46
List Price:  ISK kr11,078.31
You save:  ISK kr5,539.84
DH400.86
List Price:  DH801.83
You save:  DH400.96
L734.57
List Price:  L1,469.33
You save:  L734.75
ден2,358.54
List Price:  ден4,717.67
You save:  ден2,359.13
MOP$318.86
List Price:  MOP$637.80
You save:  MOP$318.94
N$733.28
List Price:  N$1,466.76
You save:  N$733.47
C$1,465.68
List Price:  C$2,931.72
You save:  C$1,466.04
रु5,417.88
List Price:  रु10,837.11
You save:  रु5,419.23
S/148.31
List Price:  S/296.66
You save:  S/148.35
K161.51
List Price:  K323.07
You save:  K161.55
SAR150.21
List Price:  SAR300.46
You save:  SAR150.25
ZK1,102.28
List Price:  ZK2,204.85
You save:  ZK1,102.56
L190.81
List Price:  L381.67
You save:  L190.86
Kč963.45
List Price:  Kč1,927.14
You save:  Kč963.69
Ft15,873.63
List Price:  Ft31,751.23
You save:  Ft15,877.60
SEK kr441.19
List Price:  SEK kr882.49
You save:  SEK kr441.30
ARS$40,705.74
List Price:  ARS$81,421.67
You save:  ARS$40,715.92
Bs275.23
List Price:  Bs550.54
You save:  Bs275.30
COP$174,344.67
List Price:  COP$348,732.94
You save:  COP$174,388.27
₡20,095.61
List Price:  ₡40,196.26
You save:  ₡20,100.64
L1,011.06
List Price:  L2,022.38
You save:  L1,011.31
₲310,574.38
List Price:  ₲621,226.44
You save:  ₲310,652.05
$U1,776.52
List Price:  $U3,553.49
You save:  $U1,776.96
zł163.49
List Price:  zł327.04
You save:  zł163.54
Already have an account? Log In

Transcript

Now we are going to talk about a very important concept in programming in any programming language and in JavaScript in specific, which is the concept of debugging. But first of all, what does debugging mean? The word debugging in programming means that you try to find errors in your code and work on fixing them. And the process, I have to admit that it's not easy because sometimes you have a very long code. And it's not easy to figure out exactly where the code where the error is in your code. Therefore, the good news is that once you run your code, it's gonna help you find where the errors are by displaying if there is any errors in the console, but not all of the time because as we know, computers are not smart enough to find these errors.

Computers just execute what we tell them to execute. Therefore, sometimes we have to take some effort to find where the errors are and fix them. And I remember back in the day, I was working on a project. And it took me like, one week to find where the error is. And the project was developing a mobile app, which had many algorithms. And it was very difficult to find where the error is.

But trust me if you know exactly if you have a good understanding of the basics, you will be able to find where the errors are and fix them. So here we have a code that is going to just print out all of the grades inside this. Inside this array. It's going to use the for loop to loop through this array and print out all of these grids. If I save and run, what else Gonna get, as you can imagine, all of these grades will be printed out, which are only 1234567 grades as you can see. And let's now make some errors, intentionally make some errors and see what we get.

For example here, if I delete this semicolon, if I save and run, what I'm going to get, as you can see an error, and it's going to unfortunately, it's going to tell me where exactly the error is. And this is a very good news because you know exactly where the error is. And you will be able to fix them instantaneously rather than going through a very long code and try to find where the error is by yourself. So here, it's gonna just tell you unexpected identifier and it's going to point to the exact location where your error is. So it's, it's it it's pointing just on the eye or before the eyes, just And it's gonna tell you on syntax error. So that you know, it's a syntax error.

It's not any other kind of errors like a runtime error or anything else. And unexpected identifier. And once you get to this location, and it also gives you the line here tells me line seven. As you can see, I go to line seven, and I just read my code again. And oops, I find here that I have forgotten to add the semicolon here. So just add the semicolon and save and run.

And I'm going to get, again the code I'm going to get it to be excused, executed correctly without any errors. Let's now try to intentionally make another error. If I forgot, for example, to add this closing curly braces, if I save and run, I'm going to get again I'm going to get an error and it's going to tell me again at line 13. There is an error. And it's gonna point out to the, to the location where the error is, which is here. It should be here.

And it tells me here, syntax error unexpected, unexpected ducking, here at this location. And it's like 12. But if I just move my my cursor here, and if I save and run, it's gonna point to line 10. Again, which means that the error is here. And if I go through the code again, I will immediately know that I forgot I have forgotten to the closing curly braces here. If I save and run, I'm going to get the code running again without any errors.

Another error is that when you call when you forget to add the dot, that is going to call a function. So for example, here, the grades array calls the length function, if I for if I just for forgot to add this.if I save and run, I'm going to get an error and it's going to tell me a reference error and and be careful a reference error happen when you forget when it's something something related to an object. So, here the reference error grid link is not identified, because this is not a variable here what what JavaScript thinks now is that there is a variable called our grades linked, but I have forgotten to create that variable or that object. So here there is a reference error, but the truth is that there is there is no grid link. There is only grades, variable or object and I am trying to call the link function.

So here I just forgot the.so Be careful because as you As I told you computers are not smart, they just do what we tell them to do. So here, our computer is not smart. It just is trying to find a variable called Grades link, but it's not able to find it. Therefore, it's going to give you a reference error. So here, it's pointing to this location at line seven. I'm going to get to line seven and I'm going to figure out that I, I forgot to add this.if.

I save and run, I'm going to get our code to run successfully. So these are very important errors that we usually face, especially the the semicolon, we sometimes forget to add the semicolon. We forget to add the dirt when we call up when we call functions from objects. We also forget, usually forget to use the curly braces, the parentheses, we sometimes create some syntax errors Like, if I hear instead of type, instead of typing grades, I might type just grid. If I save and run, I'm going to get an error. And it's going to tell me reference error because it's it was not able to find a variable called grid.

So here it should be great. So these are very common mistakes that a lot of programmers commit, you need to know these, these errors. And I would give you a tip here, right these errors down. And whenever you find some errors, just go go through this list of errors, and try to find which error It is so that in the future, you recognize what types of error you might face, especially when you create a very long script.

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.