|
| 1 | +When you write programs, it is sometimes useful to write comments, so you, or anyone reading the program, can understand it better. |
| 2 | + |
| 3 | +The comments are completely ignored by the program interpreter in the computer, so you can write anything on the comments and it will never cause any error. |
| 4 | + |
| 5 | +# Code Comments |
| 6 | + |
| 7 | +--- |
| 8 | + |
| 9 | +In Javascript there are two types of comments: line comments and block comments. |
| 10 | + |
| 11 | +A line comment starts when the program reaches the two forward slashes in sequence. This informs the computer that whatever follows those slashes in that line is a comment and should not be considered program instructions. For example: |
| 12 | + |
| 13 | +``` |
| 14 | +// This is a comment. The entire line is ignored by the computer when running the program. |
| 15 | +console.log(23) // This prints the number 23 on the console. Anything after the slashes is a comment. |
| 16 | +``` |
| 17 | + |
| 18 | +As you can see above, line comments can be used on lines that contain Javascript code. Anything from the slashes to the end of the line is ignored. |
| 19 | + |
| 20 | +A block comment is anything between the characters `/*` and `*/`. It can appear anywhere in a Javascript program and can span multiple lines. For example: |
| 21 | + |
| 22 | +``` |
| 23 | +/* This is a Javascript block comment. |
| 24 | + This comment spans more than one line. */ |
| 25 | +/* |
| 26 | + Anything between "whack asterisk" and "asterisk whack" is not considered actual Javascript code, and is ignored |
| 27 | + when running the program. |
| 28 | +*/ |
| 29 | +console.log(/*The answer to the fundamental question, according to a certain book*/42) // prints 42 |
| 30 | +``` |
| 31 | +As you can see above, you can mix and match both types of comments. |
| 32 | + |
| 33 | +Comments are used to explain code, call attention to parts of the program, inform about licenses and copyrights, and they can also be used to organize your thoughts before writing a program: you can write the pseudocode for your program as comments and then add the actual Javascript commands or replace the comments with commands to implement the pseudocode. |
| 34 | + |
| 35 | +You will see this in this course exercises. The exercises are contained in numbered folders under the `exercises` folder. Every exercise is one or more files that contains instructions as comments. For example, this is exercise number one (file `HelloWorld.js` in the `ex01` folder): |
| 36 | +``` |
| 37 | +// Write a program that prints the string "Hello, World!" using the console.log function |
| 38 | +// You should write your code in place of the comment below that reads // place your code here |
| 39 | +// The program below should produce the following output: |
| 40 | +// Hello, World! |
| 41 | + |
| 42 | +// place your code here |
| 43 | +``` |
| 44 | +In this case all you have to do is replace the last comment line with your code. Try doing this and running the program using Node.js. |
| 45 | + |
0 commit comments