JavaScript Comments
Comments in JavaScript are used to describe and annotate code. They are ignored by the JavaScript engine but are important for code readability and maintenance.
1. Single-Line Comments
Single-line comments are used for brief explanations or notes. They begin with //
and continue to the end of the line.
// This is a single-line comment
console.log('Hello, world!');
2. Multi-Line Comments
Multi-line comments are used for longer explanations that span multiple lines. They begin with /*
and end with */
.
/*
This is a multi-line comment
It can span multiple lines
*/
console.log('Hello, world!');
3. Role of Comments
Comments play a crucial role in making code more understandable. They can be used to explain complex logic, mark TODOs, or temporarily disable code during development.
// TODO: Refactor this function
function calculateTotal(price, tax) {
return price + tax;
}