← Back to Chapters

JavaScript Comments

? JavaScript Comments

? Quick Overview

Comments are notes you add to your JavaScript code that are ignored by the JavaScript engine. They help explain logic, document your code, or temporarily disable lines.

? Key Concepts

  • Single-line comments start with //.
  • Multi-line comments use /* ... */.
  • Comments never affect how code runs.
  • Useful for debugging or explaining logic.

? Syntax & Theory

  • // This is a single-line comment
  • let x = 5; // store the initial value
  • /* This is a multi-line comment */

✍️ Commenting Guidelines (for these notes)

To make comments stand out in your JavaScript notes, every comment inside a code block is wrapped in a special span:

  • <span class="comment">// This is a comment</span>
  • let x = 5; <span class="comment">// store the initial value</span>
  • <span class="comment">/* multi-line comment here */</span>

The CSS rule .code-block .comment makes these comments appear in a different color and italic style.

? Code Examples

? Single-line and inline comments
// This is a single-line comment
let x = 5; // This is also a single-line comment

// Calculate the total price
let price = 100;
let quantity = 2;
let total = price * quantity;
? Multi-line comments
/*
This is a multi-line comment.
It can span multiple lines.
Use it for longer explanations.
*/
let y = 10;
? Using comments for debugging
/*
Temporarily disable a line by turning it into a comment.
This is helpful while debugging or testing.
*/

// let result = complexCalculation(a, b);
console.log("Testing without calculation");

? Live Output & Explanation

?️ Console output

The console will show:

? View console output
Testing without calculation

? Quick Interactive Demo

Click the button to toggle a short comment tip:

 

? Common Use Cases

  • Explaining logic behind complex conditions.
  • Describing what a function or algorithm does.
  • TODO notes such as // TODO: handle invalid input.
  • Temporarily disabling code during testing.

? Tips & Best Practices

  • Explain why, not just what.
  • Avoid obvious comments.
  • Update outdated comments.
  • Use multi-line comments for long explanations.

? Try It Yourself

  • Wrap all comments using <span class="comment">.
  • Add a multi-line explanation to one of your scripts.
  • Experiment by removing the class to see style changes.