๐ŸŽ“ A1Learner
๐Ÿ”
0 of 40 lessons completed

Dart Comments

A comment is a note written in code for people to read. Dart ignores every comment when a program runs.

A comment above code

The note above the print explains that line. Only the greeting reaches the screen.

void main() {
  // Show a greeting
  print('Hello');
}

A comment after code

The note after the code explains that print line. The shown word is the only output.

void main() {
  print('Hello again'); // Show another greeting
}

A block comment

The block note spans two lines above the print lines. Both shown words still appear in order.

void main() {
  /* Show two short lines
     Each line shows one word */
  print('First');
  print('Second');
}

Example

void main() {
  // Show the title
  print('Morning');
  /* Show the next line
     The step shows below this note */
  print('Tea');
}

Key points

  • Use // to start a line comment.
  • A line comment runs to the end of its line.
  • Use /* to open a block comment and */ to close it.
  • A comment can sit above a line of code or after code on the same line.

In this example, a line comment sits above the first print, and that print shows Morning. A block comment sits above the second print, and that print shows Tea. No comment text reaches the screen.

Note

A program with comments gives the same output as the same program without them.

Try it Yourself

Edit the code, then press Run.

Running is simulated ยท DartPad

Exercise

What does Dart do with a comment?