What are comments and why should I care?

You sometimes need to put some text into source code, that is not to be included in the program.

To do this we can use comments.

One way of thinking about comments is that comments are invisible for the computer.

Comments often contain text that is for the benefit for the human reading the source code.

In the programming language we are using here at Nilwen, JavaScript, there are two types of comments.

Single-line comments and multi-line comments.

// A single line comment

This type of comment starts with // and ends at the end of the line.

/* A multi
   line comment */

This type of comment starts with /* and ends with */. They can span more than one line.

Comments are put to many uses in the daily work of a programmer. For example:

  • To put information about the author into the code.

  • To explain a section of the source code that is not obvious, or potentially confusing.

  • To put a reminder of stuff that is do be done.

  • To temporarily remove code during debugging.

  • And many more...

Let's try it out

1

Type the following into the the box where it says Enter source code here:

1
2
3
4
paint('green');
// paint('red');
paint('grey');
/* Hi, Mom */
2

Click Check source code.

3

Click Run!.

So what is happening here?

As you can see, the second and fourth lines contains comments and are ignored during the run of the program. The uncommented lines will be executed as normal.

When you feel that you understand the content of this lesson. You are perhaps ready for the following lessons:
The interactive story foundation
How to make an interactive story
JavaScript
What is JavaScript?