Start learning free

LESSON 3 OF 348 · WELCOME TO TYPESCRIPT

Comments in TypeScript: Single-Line, Block and Doc Comments

Lesson Read the full explanation

TL ; DR:

  • Comments are lines of code that TypeScript will intentionally ignore (not execute). They don't do anything.


  • You add a single line comment in TypeScript by typing // followed by any text:

    // This is a comment!
    console.log("This is not!");
    


Full lesson:

Comments are lines of code that TypeScript will intentionally ignore. They don't do anything.

They're just used to create notes for yourself and others about what the code does.

There are two types of code comments in JavaScript:


1. Single line comments:

We create single line comments by typing // and TypeScript will ignore (not execute) any text between // and the end of the line.

Single line comments automatically end at the next line of code:

// Next line will greet the user:
console.log("Hello user!");

Output of this code:

Hello user!

We can also use single line comments at the end of a line to explain the code:

console.log("Hello user!"); // This line greets the user
console.log("This is not a comment!");

Output of this code:

Hello user!
This is not a comment!

2. Multi-line comments:

We can also comment multiple lines of code using multi-line comments.

We type /* to begin the comment and type */ to end the comment:

/* This is a 
multi-line comment
console.log("This will not run");
Next line will greet the user:
*/

console.log("Hello user!");

Output of this code:

Hello user!

Assignment
Follow the Coding Tutorial and let's write some comments!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · WELCOME TO TYPESCRIPT

    Comments in TypeScript, explained

    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.log instead 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:

    1. Click at the very start of the code, so the new line goes above everything.
    2. Type //, a space, and the text Story of Andy.
    3. 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.
    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free