How to debug Javascript errors

It’s normal for you to make errors when you code. So if you see an error, you don’t have to be afraid. I get error messages all the time. When I have bad days, I get more error messages!

Error messages are something bad. We’re actually lucky to have e…

It’s normal for you to make errors when you code. So if you see an error, you don’t have to be afraid. I get error messages all the time. When I have bad days, I get more error messages!

Error messages are something bad. We’re actually lucky to have error messages show up infront of us when code.

Think about designers and other professions, when they create something, its hard for them to know what’s wrong with their work.

In our case, we get instant feedback through error messages. These error messages are like angels that gently tell us that something is wrong. You can correct the code and your program would work.

Let’s go through an example.

In this code, we’re trying to add an event listener to a button, and when the button is clicked, the button should turn orange.

If you hit the refresh button, you’ll see an error message. The first thing you do is to breathe and relax.

Then, you read the error message. You also look at where the message occurs.

The error message says you can’t read a property addEventListener of null. If you’re unsure whether btn is null, you can do a console.log.

Here, you’ll understand you missed a . when declaring the btn variable. What you’ll need to do is fix this querySelector declaration and your code works.

I hope this video has been helpful for you.

If you like this video, you might want to consider subscribing to this youtube channel, and I’ll send you a helpful tip about frontend development every friday.

You may also want to consider subscribing over at my blog at zellwk.com, and you’ll get one article plus one video from me every week to help you become a better frontend developer.

Now good luck, and have a nice day!


Print Share Comment Cite Upload Translate
APA
Zell Liew | Sciencx (2024-03-28T15:06:51+00:00) » How to debug Javascript errors. Retrieved from https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/.
MLA
" » How to debug Javascript errors." Zell Liew | Sciencx - Friday November 17, 2017, https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/
HARVARD
Zell Liew | Sciencx Friday November 17, 2017 » How to debug Javascript errors., viewed 2024-03-28T15:06:51+00:00,<https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/>
VANCOUVER
Zell Liew | Sciencx - » How to debug Javascript errors. [Internet]. [Accessed 2024-03-28T15:06:51+00:00]. Available from: https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/
CHICAGO
" » How to debug Javascript errors." Zell Liew | Sciencx - Accessed 2024-03-28T15:06:51+00:00. https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/
IEEE
" » How to debug Javascript errors." Zell Liew | Sciencx [Online]. Available: https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/. [Accessed: 2024-03-28T15:06:51+00:00]
rf:citation
» How to debug Javascript errors | Zell Liew | Sciencx | https://www.scien.cx/2017/11/17/how-to-debug-javascript-errors/ | 2024-03-28T15:06:51+00:00
https://github.com/addpipe/simple-recorderjs-demo