Online Compiler logoOnline Compiler

JavaScript Tutorial

Comments in JavaScript (Complete Guide)

Comments in JavaScript are non-executable lines used to explain, document, or temporarily disable code. They are ignored by the JavaScript engine but are essential for readability, maintainability, and collaboration.

Why this matters

Whether you build small scripts or large applications in React/Next.js, effective comments are a professional habit that improves team velocity and code clarity.

What Are Comments in JavaScript?

A comment is text in code that the engine ignores during execution.

  • Explain what code does
  • Describe logic and intent
  • Document functions/modules
  • Help other developers understand implementation
  • Temporarily disable lines during debugging

Types of Comments in JavaScript

JavaScript supports three main comment styles:

  • Single-line comments (//)
  • Multi-line comments (/* */)
  • Documentation comments (JSDoc: /** */)

Single-Line Comments (//)

Single-line comments begin with // and apply to the rest of that line.

  • Use for quick notes
  • Use for one-line explanations
  • Useful during fast debugging

Multi-Line Comments (/* */)

Multi-line comments span across lines and are useful for longer descriptions.

  • Use for detailed explanation
  • Use for temporarily commenting blocks
  • Always close the comment properly

Documentation Comments (JSDoc)

JSDoc comments provide structured metadata for functions/classes and improve tooling support in editors.

  • Better IntelliSense and type hints
  • Auto-generated documentation support
  • High value in larger team codebases

Why Comments Are Important

  • Improve readability
  • Help during debugging
  • Improve team collaboration
  • Capture business logic context

Keyboard Shortcuts (VS Code)

  • Single-line comment: Ctrl + / (Windows), Cmd + / (Mac)
  • Multi-line comment: Shift + Alt + A (selected lines)

Best Practices for Writing Comments

  • Write meaningful comments, not obvious ones
  • Explain why, not only what
  • Keep comments updated with code changes
  • Use clear grammar and concise language
  • Document complex logic and business rules

When Not to Use Comments

Avoid commenting obvious lines that are already clear from good naming and structure.

Prefer expressive code first, then comments where necessary.

Common Mistakes with Comments

  • Forgetting to close multi-line comments
  • Leaving outdated comments after refactors
  • Over-commenting every line
  • Commenting broken code instead of fixing root cause

Summary

Comments do not affect execution, but they improve readability, debugging, collaboration, and documentation quality.

Three key styles: single-line (//), multi-line (/* */), and JSDoc (/** */).

Mature developers prioritize clear code and add comments where they bring real value.

Code Examples

Single-Line Comment

// This is a single-line comment
console.log("Hello World"); // Prints greeting message

let total = 100; // Initial total amount

Everything after // on the same line is ignored by JavaScript.

Multi-Line Comment

/*
  This is a multi-line comment.
  It can span multiple lines.
*/
console.log("Learning JavaScript");

/*
  Calculate final price
  after applying discount and tax
*/
let finalPrice = price - discount + tax;

Use this format for longer explanations or temporary block-level disabling.

JSDoc Comment

/**
 * Adds two numbers
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
function add(a, b) {
  return a + b;
}

JSDoc improves editor hints and documentation quality in professional projects.

Professional Real-World Example

"use strict";

/**
 * Calculates discount based on user type
 * @param {string} userType
 * @param {number} price
 * @returns {number}
 */
function calculateDiscount(userType, price) {
  // Premium users get 20% discount
  if (userType === "premium") {
    return price * 0.8;
  }

  // Regular users get 10% discount
  return price * 0.9;
}

Demonstrates strict mode, JSDoc, and meaningful logic comments together.

Common Mistakes and Fixes

Commenting obvious code

Prefer expressive naming and clean structure; comment only where extra context is needed.

Outdated comments after code changes

Update comments during refactors to avoid misleading documentation.

Unclosed block comment

Always close /* ... */ properly to avoid syntax errors.

Frequently Asked Questions

Do comments affect JavaScript execution speed?

No. Comments are ignored by the engine and do not execute.

Should I comment every line of code?

No. Comment intent and complex logic, not obvious statements.

When should I use JSDoc?

Use JSDoc for shared modules, utility functions, and larger codebases where API clarity matters.

Can comments help during debugging?

Yes. Temporarily disabling lines and adding context comments can speed up debugging.

Related JavaScript Topics