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.
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.
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.