TypeScript Comments

Note: If you're new to TypeScript, check our Getting Started with TypeScript tutorial first.


TypeScript comments are notes in the code that are completely ignored by the compiler.

Here's a simple example of comments in TypeScript. You can read the rest of the tutorial to learn more.

Example

// Display "Programiz" to the screen
console.log("Programiz");

// Output: Programiz

Here, // Display "Programiz" to the screen is a comment. As a result, it is ignored by the TypeScript engine.


Types of Comments in TypeScript

In TypeScript, there are two ways to add comments to code:

  1. // - Single-Line Comments
  2. /* */ - Multiline Comments

Single Line Comments

In TypeScript, any line that starts with // is a single-line comment. For example,

let name: string = "John Doe";

// Display name on the console
console.log("Hello " + name);

Output

Hello John Doe

Here, // Display name on the console is a single-line comment that describes what console.log("Hello " + name); is doing.

Notice that the comment is above console.log(). In TypeScript, you can also use single-line comments right beside the code:

let name: string = "John Doe";

console.log("Hello " + name);  // Display name on the console

However, avoid using comments this way if they are long and descriptive.


Multiline Comments

In TypeScript, multiline comments allow you to add comments that can span multiple lines. They start with /* and end with */. For example,

/* Create a string variable
and print it to the console
*/

let name: string = "John Doe";

console.log(name);

Here, we have used a multiline comment that can span any number of lines.

Note: You can enclose a single-line comment within /* and */ but it's much more convenient to just use //.


Comments for Removing Unwanted Code

We can use comments to remove unwanted code that we might require later.

Comments can be helpful if you want to remove an unwanted line of code that can still be useful in the future. Consider the program below:

console.log("Hello John Doe!");
console.log("Welcome to our TypeScript tutorial.");

Suppose the line console.log("Welcome to our TypeScript tutorial."); isn't required right now. But you know you might change your mind in the future.

In this case, you can simply convert the unnecessary line into a comment instead of deleting it from your program.

console.log("Hello John Doe!");
// console.log("Welcome to our TypeScript tutorial.");

You can then uncomment the code whenever you need to use it.


Why Use Comments?

As a TypeScript developer, you'll write code and also need to update or review code written by others.

If you write comments on your code, it'll be easier for you (and your fellow developers) to understand it in the future.

As a general rule of thumb, use comments to explain why you did something rather than how you did something.

Notes:

  • Comments shouldn't be used for explaining poorly written code. Your code should always be well-structured and self-explanatory.
  • Remember the shortcut for using comments; it can be extremely helpful. For most code editors, it's Ctrl + / for Windows and Cmd + / for Mac.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges