Language: EN

comentarios-en-javascript

Comments in JavaScript

Comments are text fragments that include notes and explanations in the source code that are ignored by the compiler or interpreter during execution.

Their purpose is to provide explanations or notes for developers reading the code, whether 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:

  1. Single-line comments: Used for brief comments that extend to the end of a line.
  2. Multi-line comments: Used for lengthier comments that span multiple lines.

Single-line comments

Single-line comments begin 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 = "Juan"; // It can also be used at the end of a line of code

In the previous example,

  • The comment // This is a single-line comment explains what follows,
  • The comment at the end of the line of code // It can also be used at the end of a line of code provides 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 previous example, the comment between /* and */ explains that the comment can span multiple lines and is suitable for more extensive notes.