freeCodeCamp/guide/english/javascript/comments/index.md

3.0 KiB

title
Comments

Comments

Programmers use comments to add hints, notes, suggestions, or warnings to their source code; they have no effect on the actual output of the code. Comments can be very helpful in explaining the intent of what your code is or should be doing.

It is always best practice when starting out to comment more often than not, as it can help those reading your code to understand what exactly your code is intending to do.

JavaScript has two ways of assigning comments in its code.

The first way is the // comment; all text following // on the same line is considered as a comment. For example:

function hello() {
  // This is a one line JavaScript comment
  console.log("Hello world!");
}
hello();

The second way is the /* */ comment, which can be used for both single-line and multi-line comments. For example:

function hello() {
  /* This is a one line JavaScript comment */
  console.log("Hello world!");
}
hello();
function hello() {
  /* This comment spans multiple lines. Notice
     that we don't need to end the comment until we're done. */
  console.log("Hello world!");
}
hello();

The third way is the /** */ comment, a format popularly known as JSDoc, can be used to clearly describe functions, classes, methods, and variables in your codebase in a detailed way. For example:

/**
 * Adds two numbers together
 * 
 * @param {number} num1 - first parameter
 * @param {number} num2 - second parameter
 * @returns {number}
 */
function addTwoNumbers(num1, num2) {
  return num1 + num2;
}
console.log(addTwoNumbers(10,20)) // will print 30 in the console.

You can also prevent execution of Javascript code just commenting the code lines like this:
```javascript
function hello() {
  /*console.log("Hello world!");*/
}
hello();

More Information:

How To Write Comments in JavaScript

Many IDEs come with a keyboard shortcut to comment out lines.

  1. Highlight text to be commented
  2. Mac: Push Command(Apple Key) & "/"
  3. Windows: Push Control & "/"
  4. You can also uncomment code by doing the same steps
A shortcut to comment out a section of javascript in many code editors is to highlight the lines of code you want to comment out, then press `Cmd/Ctrl + /`.

Comments are also very helpful for code testing as you can prevent a certain code-line/block from running

function hello() {
  // The statement below is not going to get executed
  // console.log('hi')
  }
hello();
function hello() {
  // The statements below are not going to get executed
  /*
  console.log('hi');
  console.log('code-test');
  */
}
hello();

More Information: