Comments
As programs get bigger and more complicated, they get more difficult to read. Good programmers try to make their code understandable to others, but it is still tricky to look at a large program and figure out what it is doing and why.
Best practice encourages us to add notes to our programs, which clearly explain what the program is doing. These notes are called comments.
A comment is text within a program intended only for a human reader—it is
completely ignored by the compiler or interpreter. In JavaScript, the //
token starts a comment, and the rest of the line gets ignored. For comments
that stretch over multiple lines, the text falls between the symbols
/* */
.
Experiment with Comments
In order to launch Visual Studio Code from the command line as referenced in the section below you will need to install the code
command in your PATH. The following articles will help you navigate this process:
Launching Visual Studio Code from your terminal with the command code .
will open Visual Studio Code with all files and directories at your current(relative) location.
You may also find the following Tips and Tricks section useful.
On your own computer, in
javascript-projects
, locate thehow-to-write-code
folder. Within that folder, you will find an example calledComments.js
. You should use the terminal to do so. Here are the steps:- Use
cd
to locatejavascript-projects
. If your work is inLaunchCode/Unit1
, then the command would becd LaunchCode/Unit1
. Run the command,ls
, to make sure that you have foundjavascript-projects
. - To navigate inside the
how-to-write-code
folder, use the commandcd javascript-projects/how-to-write-code
. - Open up the required example in Visual Studio Code by running the command,
code Comments.js
.
- Use
You are ready to add and remove comments! Here is where the code starts out at:
1 2 3 4 5 6 7 8 9 10 11 12
// This demo shows off comments! // console.log("This does not print."); console.log("Hello, World!"); // Comments do not have to start at the beginning of a line. /* Here is how to have multi-line comments. */ console.log("Comments make your code more readable by others.");
Try removing or un-commenting some code first. Then try adding both a single-line and multi-line comment.
TipRemember to run the program, you should use the command,
node comments
.
Notice that when you first run the program, it still prints the phrase Hello, World!
, but none of the comments appear. Also notice the blank lines left in
the code, which are also ignored by the compiler. Comments and blank lines make
your programs much easier for humans to understand. Use them frequently!