Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Comments | TypeScript Fundamentals
course content

Course Content

Introduction to TypeScript

CommentsComments

Q: What does a programmer do when they need to write documentation for their code?

A: They write it in a Word file.

Q: What does a good programmer do when they need to write documentation for their code?

A: They use comments.

Let's turn you into a good programmer and also teach you how to use comments within your code. But first, let's find out what they are:

Comments in code are textual or annotation notes added by developers to the source code of a program. They are not executed by the computer and are ignored during program execution. Comments are used to explain the code, making it more understandable for other developers or for future reference.

In TypeScript, there are two types of commenting:

  1. We can comment out an entire line using //;
  2. We can also comment out a block of code spanning multiple lines or a single-line code fragment using /* code */.

Let's take a look at an example where we need to comment out some lines and write comments there. I'll repeat, the compiler won't execute or even look at the commented-out code fragments, so we can write anything there (except for bad words; my mom scolds me if I write those).

As you can see in the code above, we have commented out various text fragments. In comments, we write explanations of what is happening in the code and what certain lines do. Notice that when commenting out multiple lines, we use a different comment syntax. (/* */) After //, everything to the right of the characters is commented out.

We can also comment out code fragments. For example, if we suspect an error in a particular line, we can comment out that line and run the code again to check that line for errors.

Above, we have commented out code fragments in which we suspect errors using different types of comments. Personally, I often identify error-prone areas in code using this method, and I recommend that you do the same. It's a useful skill for a good programmer.

Also, don't forget to write documentation for your code. It's like placing a bottle of water next to your bed after a night of drinking.

question-icon

What is the correct syntax for commenting? (Select multiple answers)

Select a few correct answers

Everything was clear?

Section 1. Chapter 6
course content

Course Content

Introduction to TypeScript

CommentsComments

Q: What does a programmer do when they need to write documentation for their code?

A: They write it in a Word file.

Q: What does a good programmer do when they need to write documentation for their code?

A: They use comments.

Let's turn you into a good programmer and also teach you how to use comments within your code. But first, let's find out what they are:

Comments in code are textual or annotation notes added by developers to the source code of a program. They are not executed by the computer and are ignored during program execution. Comments are used to explain the code, making it more understandable for other developers or for future reference.

In TypeScript, there are two types of commenting:

  1. We can comment out an entire line using //;
  2. We can also comment out a block of code spanning multiple lines or a single-line code fragment using /* code */.

Let's take a look at an example where we need to comment out some lines and write comments there. I'll repeat, the compiler won't execute or even look at the commented-out code fragments, so we can write anything there (except for bad words; my mom scolds me if I write those).

As you can see in the code above, we have commented out various text fragments. In comments, we write explanations of what is happening in the code and what certain lines do. Notice that when commenting out multiple lines, we use a different comment syntax. (/* */) After //, everything to the right of the characters is commented out.

We can also comment out code fragments. For example, if we suspect an error in a particular line, we can comment out that line and run the code again to check that line for errors.

Above, we have commented out code fragments in which we suspect errors using different types of comments. Personally, I often identify error-prone areas in code using this method, and I recommend that you do the same. It's a useful skill for a good programmer.

Also, don't forget to write documentation for your code. It's like placing a bottle of water next to your bed after a night of drinking.

question-icon

What is the correct syntax for commenting? (Select multiple answers)

Select a few correct answers

Everything was clear?

Section 1. Chapter 6
some-alt