← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 6 of 12

JS Comments

Imagine leaving sticky notes on a shared project at work, explaining why a decision was made, warning a colleague about a tricky part, or temporarily crossing out a step you're not using right now. Comments in JavaScript are exactly that, notes written into your code that the browser completely ignores when running the script, but that are incredibly valuable to any human reading the code later, including future you. Good comments explain the why behind a decision, not just the what, and international teams working on projects like cookiescursor.com rely on clear comments to stay in sync across different time zones.

Single Line Comments

A single line comment starts with two forward slashes //, and everything from that point to the end of the line is ignored by the JavaScript engine.

Note: Use single line comments for short, quick notes right above or beside the line of code they explain.

Warning: A // inside a string, like a URL, is just text and does not start a comment, comments only apply to actual code.

Example: Single Line Comments

javascript
// This line explains the code below
let age = 25;
console.log(age);

Multi Line Comments

A multi line comment starts with /* and ends with */, letting a comment span several lines, which is useful for longer explanations or temporarily disabling a block of code.

Note: Use multi line comments for longer explanations, like describing the purpose of an entire function or file.

Warning: Multi line comments cannot be nested, starting a new /* inside an existing comment does not create an inner comment and can cause confusing errors.

Example: Multi Line Comments

javascript
/*
  This is a longer explanation
  spanning multiple lines.
*/
console.log("Done");

Commenting Out Code

Comments are commonly used to temporarily disable a line or block of code without deleting it, which is useful while debugging or testing alternate approaches.

Note: Most code editors let you comment out a selected block with a keyboard shortcut, making this technique fast to apply while testing.

Warning: Remember to remove or restore commented-out code once you're done testing, leaving it in permanently makes the file confusing for others.

Example: Commenting Out Code

javascript
console.log("This runs");
// console.log("This is disabled for testing");

Writing Useful Comments

The most valuable comments explain why a piece of code exists or why a particular approach was chosen, since the code itself already shows what it does, comments should add context that isn't obvious from reading it alone.

Note: Before writing a comment, ask whether it explains something the code doesn't already make clear, if not, it may be unnecessary.

Warning: Comments that simply restate the code, like '// set x to 5' above 'const x = 5;', add clutter without adding real value.

Example: Writing Useful Comments

javascript
// Using 0.9 because the API rounds up otherwise
const threshold = 0.9;
console.log(threshold);

JSDoc Basics

JSDoc is a structured comment style, starting with /**, that documents a function's purpose, parameters, and return value in a standardized format many editors and tools can read to provide helpful hints.

Note: Even a simple JSDoc comment above a function can make autocomplete suggestions in your editor far more helpful.

Warning: JSDoc comments must start with exactly /** (two asterisks) to be recognized by tools, a single /* is treated as a regular comment.

Example: JSDoc Basics

javascript
/**
 * Adds two numbers together.
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
function add(a, b) {
  return a + b;
}
console.log(add(2, 3));
Common Mistakes
  1. Writing comments that just repeat what the code obviously does, instead of explaining why it does it.
  2. Forgetting to close a multi-line comment with */, which accidentally comments out all the code that follows.
  3. Leaving large blocks of old, commented-out code in a file indefinitely, cluttering it instead of deleting unused code.
Chapter Summary
  • Single-line comments start with //, and everything after them on that line is ignored.
  • Multi-line comments are wrapped in /* and */, and can span as many lines as needed.
  • Comments are useful for explaining tricky logic and temporarily disabling code while testing.
Browser Support

Both // and /* */ comment syntax are part of core JavaScript and are supported identically by every browser and JavaScript engine.

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.