Comments are pieces of text that include notes and explanations in the source code which are ignored by the compiler or interpreter during execution.
Their purpose is to provide explanations or notes for developers reading the code, either at the time of writing it or in the future, when it needs to be modified or maintained.
In JavaScript, comments can be of two types:
- Single-line comments: Used for brief comments that extend to the end of a line.
- Multi-line comments: Used for more extensive comments that span multiple lines.
If you want to learn more, check out the Introduction to Programming Course
Single-line Comments
Single-line comments start with two forward slashes (//). All text following these slashes on the same line is considered a comment and is ignored by the JavaScript interpreter.
// This is a single-line comment
let name = "Luis"; // It can also be used at the end of a line of code
In the example above,
- The comment
// This is a single-line commentexplains what follows, - The comment at the end of the code line
// It can also be used at the end of a line of codeprovides an additional note.
Multi-line Comments
Multi-line comments are enclosed between /* and */. All text within these delimiters is considered a comment and is ignored by the interpreter.
/*
This is a multi-line comment.
It can span multiple lines.
It is useful for longer explanations or to disable blocks of code.
*/
let age = 30;
In the example above, the comment between /* and */ explains that the comment can span multiple lines and is suitable for more extensive annotations.
