Dart Comments
Dart Comments
A comment is text in your code that Dart ignores.
You can use comments to explain your code and to make it easier to read.
You can also use comments to stop some code from running while you test other code.
Single-line Comments
Single-line comments start with two forward slashes //.
Dart ignores everything from // to the end of the line.
This example uses a comment before a line of code:
Result:
Hello World
This example uses a comment at the end of a line of code:
Result:
Hello World
Comment Out Code
If you put // in front of a line of code, Dart does not run that line.
This is called commenting out code:
Result:
Apples
Cherries
Multi-line Comments
Multi-line comments start with /* and end with */.
Dart ignores all text between them, even if it covers many lines:
Example
void main() {
/* The code below prints
Hello World to the screen,
and it is amazing */
print('Hello World');
}
Try it Yourself »
Result:
Hello World
You can also use /* */ to hide a small part of a line:
Result:
40
Tip: Most Dart developers use // for short comments, even when the comment covers several lines.
Documentation Comments
A comment that starts with three slashes /// is a documentation comment.
You write it just before a function, a class or a variable, to describe what it does.
Code editors show this text when you point at the name, and tools can turn it into web pages:
Example
/// Prints a friendly welcome message.
void main() {
print('Welcome to Dart!');
}
Try it Yourself »
Result:
Welcome to Dart!
Like other comments, documentation comments do not change what the program does.