ProgramGuru

TypeScript Comments

Learn TypeScript Comments

Comments communicate intent to readers, while executable statements determine the program output.

In this lesson, you will study the rule, run a complete TypeScript example, explain its output, and test a small change.

What you’ll learn

  • Explain the main rule behind typescript comments.
  • Predict the result of the complete runnable TypeScript example.
  • Recognize one common mistake and correct it.

Understand the concept

Use // for a single-line comment and /* ... */ for a block comment. JSDoc-style comments can also describe APIs for tooling. Ordinary comments do not change evaluated expressions, and comments should explain why code exists rather than restating every line.

Run a complete TypeScript example

Save this code as main.ts or paste it into the TypeScript Playground. The example uses fixed values so it runs without prompting for input.

TypeScript code

// Price is expressed in whole rupees.
 const price: number = 150;
 /* Apply a fixed delivery charge for this example. */
 const delivery: number = 30;
 console.log(price + delivery);

Verified output

180

How to run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. When using a project configuration, run the matching project build instead.

Trace the execution

The two comments are ignored during execution. price and delivery are added and the total is printed.

Predict the output before you run the sample. Compare each line with the source expression that produced it, and distinguish the type checker’s work from what happens in JavaScript at runtime.

Common mistakes and how to avoid them

Do not put a necessary calculation only in a comment. Readers may see it, but the runtime will not execute it.

Debugging habit: when a change is rejected, read the exact compiler diagnostic and inspect the operand types. When it compiles but behaves unexpectedly, follow the values through the JavaScript expressions.

Practice it yourself

Add a comment explaining why you might offer free delivery above a threshold, then implement that threshold in real code.

Challenge: write down your prediction first, run the changed source, and explain any difference. Return to the verified version before continuing.

Check your understanding

What does a // comment do?
Choose one answer. You can try again.

Answer and explanation

Documents text without executing it as an instruction.

Comments are for developers and documentation tools, not ordinary program control.

Review the key idea

TypeScript Comments: Use // for a single-line comment and /*.

Comments are for developers and documentation tools, not ordinary program control.

Before continuing, be able to explain what is checked before execution and what actually changes at runtime in the example above.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Variables in TypeScript →