Skip to main content

Archive

Show more

How to Comment Out Multiple Lines in JavaScript

How to Comment Out Multiple Lines in JavaScript

In JavaScript, you can use block comments to comment out multiple lines of code at once. This is especially useful when you want to temporarily disable a section of code or add descriptive notes without having to comment each line individually.


Using Block Comments

Block comments in JavaScript are created using the /* and */ syntax. Everything between these markers will be treated as a comment and ignored by the JavaScript interpreter.


/*
This is a block comment in JavaScript.
You can use it to comment out multiple lines of code.

For example:
console.log('This line will not be executed.');
console.log('Neither will this line.');
*/

In this example:

  • All the lines between /* and */ are commented out and will not be executed.
  • You can use this method to comment out any number of lines.

Example of Using Block Comments

Here’s a practical example where block comments are used to temporarily disable a section of code:


function add(a, b) {
    /*
    const result = a + b;
    console.log(result);
    */
    return a + b;
}

console.log(add(5, 3)); // Output: 8

In this example:

  • The lines inside the block comment are disabled, so the console.log(result) line will not run.
  • The function add still returns the sum of a and b, but the intermediate console.log is skipped.

Conclusion

Block comments are a powerful feature in JavaScript that allow you to comment out multiple lines of code easily. They are useful for debugging, disabling code temporarily, and adding detailed explanations. Remember to use them wisely to keep your code clean and readable.

Comments