(click anywhere to close)

[JavaScript] Debugging

category: Website | course: JavaScript | difficulty:

A large part of programming, often seen as the most annoying part, is a process called debugging. It’s impossible to do everything right the first time, which means your scripts will regularly not work as expected or not work at all. To be able to see what is happening behind the scenes, every browser has a console. Because the browser is what executes your JavaScript, it also automatically contains a package called developer tools, which the console is part of. To open it up, you can usually find it in the menu under Tools, or press F12.


To communicate with this console, and log some text to it, we use the console.log(message) command. This message can be anything you want from your script, and you can use it to check the values of variables or whether something failed or succeeded by placing them throughout your code. Once you got what you were looking for though, I recommend removing the call to keep the code clean.

console.log("Hello World!");

You can type any JavaScript statements directly into the console. This is very useful for quick testing.


In order to tell yourself what you mean by certain bits of code, or how they work, you can leave comments for yourself in the file. These are ignored by the browser and have no influence at all on the script. Single-line comments are created by typing // comment, multiline comments are created with /* comment */.

//I am a comment

I am a comment
I span multiple lines

It’s also commonly used for finding out where the problem is, or what code works best. You can quickly comment out parts of the code, and then rerun the script to see if that has solved or changed anything.

Error Handling

Computers can’t sense what you meant to do when you wrote something. It will therefore only throw errors when there’s something wrong with your syntax, for example a typo or an impossible statement, and not when something doesn’t work the way you want.

Errors appear in the console, and usually specify what went wrong and where it happened, making it easy to solve most of them. They are quite generic, however, and there are specific code blocks you can use for ‘catching’ errors, which you’ll learn about later in the course. If no errors are thrown, but the script doesn’t do what you want it to, there’s something wrong with your own logic.

JavaScript will stop executing the script if it finds an error it can’t overcome – which happens more often than not – and just continue and skip this particular statement otherwise. The advantage is that you can’t write sloppy code, the disadvantage is that your whole program falls apart with the slightest misstep. I therefore recommend testing what you’re writing very regularly!

Do you like my tutorials?
To keep this site running, donate some motivational food!
Chocolate Milk