JavaScript ยท Chapter 6 of 55

JavaScript Comments

Comments let you annotate code for humans; the JavaScript engine ignores them completely. Good comments explain WHY code exists, not just WHAT it does.

JavaScript supports single-line comments with `//` and multi-line comments with `/* ... */`.

Single-line comments

Everything after `//` on a line is ignored. Use them for brief notes next to code.

Multi-line comments

Wrap longer explanations in `/* */`. These are also used to temporarily disable blocks of code during debugging.

Example 1 (javascript)
// Calculate total price
let total = 100 * 1.2; // apply 20% tax
console.log(total);
Output
120

Both comment styles appear here.

Example 2 (javascript)
/*
  This function greets a user
  by name.
*/
function greet(name) {
  return "Hi " + name;
}

A multi-line comment documents the function above it.

Key points

  • `//` starts a single-line comment.
  • `/* ... */` wraps multi-line comments.
  • Comments are ignored entirely at runtime.
  • Use comments to explain intent, not obvious code.
๐Ÿ’ก Note: Avoid leaving large blocks of commented-out code โ€” use version control history instead.

๐Ÿ“ Quick Quiz

1. Which starts a single-line comment?

2. Which wraps a multi-line comment?

3. Are comments executed by the JS engine?