On this page11 sections
  1. Prerequisites
  2. Learning Objectives
  3. 1. Single-Line Comments
  4. 2. Multi-Line Comments
  5. 3. Documentation Comments
  6. 4. Good Comments
  7. Code Examples
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 005 — Comments

Category2 — Getting Started
Lecture005 of 057
Estimated video length8 min
TargetDart 3.12
LevelBeginner

Comments are notes in your code that Dart ignores. We learn the three kinds and when each one is useful.

Contents

Prerequisites · Objectives · Lesson · Code Examples · Common Mistakes · Practice Questions · Answers · Takeaways


Prerequisites

Lecture 004: you can write and run main with print.


Learning Objectives

  • Write single-line, multi-line and documentation comments
  • Use comments to switch code off while testing
  • Write comments that actually help

1. Single-Line Comments

Everything after // to the end of the line is ignored.

dart
// Prints a welcome message
print('Welcome!'); // a comment can also follow code

2. Multi-Line Comments

Everything between /* and */ is ignored, however many lines.

dart
/*
  This program prints the class schedule.
  Written for Day 2 of Dart Basic.
*/

3. Documentation Comments

Three slashes, ///, document the thing right below them. Editors show the text when you hover over its name.

dart
/// Prints a greeting for the given day.
void greet() {
  print('Good morning!');
}

4. Good Comments

Write comments that explain why, not what — the code already says what.

dart
// Bad: prints the total
print(total);

// Good: shown before the receipt so the cashier can confirm it
print(total);

Code Examples

Example 1 — Switching a Line Off

dart
void main() {
  print('Welcome to Dart Basic');
  print('Lecture 005');
  // print('This line is switched off');

  /*
  print('So is this one');
  print('And this one');
  */

  print('Goodbye!');
}

Output

code
Welcome to Dart Basic
Lecture 005
Goodbye!

"Commenting out" is a quick way to test a program without some lines — without deleting them. In IntelliJ, Ctrl + / (⌘ + / on macOS) comments or uncomments the selected lines.


Common Beginner Mistakes

1. Forgetting to close /* — without */, the rest of the file becomes a comment and you get confusing errors far below.

2. Commenting every line — // print hello above print('hello') adds nothing. Comment the reason, not the obvious.

3. Leaving old code commented out forever — once you are sure you don't need it, delete it.


Practice Questions

  1. Which symbol starts a single-line comment?
  2. How do you write a comment over several lines?
  3. What is special about ///?
  4. What happens to a line that starts with // when the program runs?

Practice Answers

  1. //.
  2. /* ... */.
  3. It documents the code below it; editors show it on hover.
  4. Nothing — Dart ignores it.

Key Takeaways

  • // comments one line; /* */ comments a block.
  • /// documents code for editors and tools.
  • Comment out lines to switch them off without deleting.
  • Good comments explain why, not what.