What comments are for
A comment is text in your source code that TypeScript ignores. It doesn't run, doesn't print and doesn't change what the program does. Comments exist for people: they explain what a piece of code is for, why a choice was made, or what a tricky line does.
Good comments say things the code cannot say by itself, such as the reason behind a decision. A comment that repeats the code word for word, like // print the name above console.log(name), adds nothing.
Single-line comments
Two slashes, //, start a single-line comment. Everything from the slashes to the end of that line is ignored. The comment ends at the line break, and the next line is ordinary code again:
// Next line will greet the user:
console.log("Hello user!");
Hello user!
A comment can also follow code on the same line:
console.log("Hello user!"); // This line greets the user
console.log("This is not a comment!");
Hello user!
This is not a comment!
Multi-line comments
To comment out several lines, start with /* and end with */. Everything between the two markers is ignored, including line breaks:
/* This is a
multi-line comment
console.log("This will not run");
*/
console.log("Hello user!");
Hello user!
A block comment can even sit in the middle of a line, as in add(2, /* the second */ 3), though this is rare.
Comments cannot be nested
A block comment ends at the first */ it finds. Writing a block comment inside another one therefore ends the outer one early, and the rest of the text becomes code. In a test, /* outer /* inner */ still outer? */ produced several compiler errors, because the text after the first */ is not valid code. If you need to disable code that already contains a block comment, use // on each line instead.
An unclosed block comment
If you start a block comment and never close it, everything after it is swallowed, and the compiler reports '*/' expected at the end of the file.
Doc comments
A block comment that starts with two stars, /** ... */, is a doc comment. Editors show its text when you hover over the function or variable that follows it:
/** Adds two numbers. */
function add(a: number, b: number): number {
return a + b;
}
To the compiler it is still a comment and has no effect on how the program runs.
Slashes inside strings
Comment markers only count when they are in the code itself. Inside a string, // is just two characters:
console.log("Visit // not a comment");
Visit // not a comment
Common mistakes
- Using
console.loginstead of a comment. You want the text ignored, not printed, so it must start with//. - Using the wrong marker.
#, which some other languages use for comments, is not a comment in TypeScript. - Forgetting to close
/*. The whole rest of the file becomes comment. - Leaving old code commented out forever. Commenting out code is useful while experimenting. Delete it once you are sure you don't need it, because version control keeps the history.
The exercise
The code in the editor already has several console.log lines that tell a short story. The task is to document it by adding a comment on line 1, above the existing lines:
- Click at the very start of the code, so the new line goes above everything.
- Type
//, a space, and the textStory of Andy. - Leave the existing lines unchanged, so they still print as before.
The finished first line is // Story of Andy. It must start with the slashes: console.log("Story of Andy") would print the text, which is not what the lesson wants.
Key takeaways
//comments out the rest of the line./* ... */comments out everything between the markers, over any number of lines, and cannot be nested./** ... */is a doc comment that editors display.- Comments are for people. They have no effect on what the program does.