Comments in JavaScript
Comments are a way for you to add context to your code and more explanation.
Comments allow you to block out an area of code, so that it won’t be executed, but will still be preserved in a document. This could be a great thing to do if you are doing homework and want to check if another line of code would work, without erasing the first thing you wrote.
In JavaScript you write a comment by using two forward slashes:
//comment goes here
The comment could be used to explain the code:
var teamPlane = “9:00am”; //time of team’s plane departure
I could write:
document.write(“The team’s plane will depart at “ + teamPlane + “.”);
I might not understand what I meant by naming the variable teamPlane above, but by adding the comment it’s clear to me and to others who might view the code. I encourage students to use comments in their code because it helps you explain what you mean, and when your code gets pretty involved, you will need comments to help you understand your own thinking—especially when you go away for a while, then come back to the code.
Also, teachers, just like in math, might appreciate the documentation that really explains your line of thinking when they review your code.
When it comes to comments everything after the two forward slashes / / is ignored, when placing your comment on a single line.
If you want to use a multi-line comment you use:
/* multi-line comments can go here and be ignored by the computer */
Everything between the /* and the */ is ignored. That way you can preserve the code (simply comment it out), when it’s on multiple lines and still have it for review if you need it.
// single-line comments
/* multi-line comments */