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

Зміст курсу

Introduction to TypeScript

Introduction to TypeScript

1. TypeScript Fundamentals
2. Conditional Statements
3. Arrays
4. Loops
5. Functions

book
Comments

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:

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).

12345678
console.log("Hello world!") // This code performs output of information to the screen. let word : string = 'c<>definity' console.log(`Hello ${word}!`) /*This code creates a variable of type string and uses it for output to the screen. We use this variable with the required syntax and successfully display text on the screen with the variable. */
copy

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.

123
//var str = 'Hello World"; var num = 132; console.log(num/* - str*/);
copy

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.

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

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

Виберіть кілька правильних відповідей

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 6
We're sorry to hear that something went wrong. What happened?
some-alt