JavaScript Comments

JavaScript Comments

Learn how to use comments in JavaScript to write clean, readable, and professional code.

This guide covers JavaScript single-line and multi-line comments with syntax, examples, best practices, and real-world use cases for clean and maintainable code.

JavaScript is one of the most widely used and essential programming languages in modern web development. It powers interactive websites, web applications, and dynamic user experiences across the internet. Whether you are a beginner learning the basics or an experienced developer building large-scale applications, writing clean, readable, and well-documented code is a fundamental skill.

One of the most effective ways to improve code readability and maintainability is by using JavaScript comments. Comments allow developers to describe what the code does, explain complex logic, and clarify why certain decisions were made during development.

Well-written comments are especially important in professional environments, team projects, and long-term applications, where code may be reviewed, updated, or maintained by other developers in the future.

Definition of JavaScript Comments

JavaScript comments are non-executable lines of text written within JavaScript code. They are completely ignored by the JavaScript engine during execution and do not affect the program’s output. Comments are used to provide explanations, document functionality, leave reminders, or temporarily disable sections of code for testing and debugging purposes.

What Are JavaScript Comments?

JavaScript comments are non-executable pieces of text written within the source code. They are ignored by the JavaScript engine and exist solely to help developers understand, organize, and maintain their code more effectively. Comments play a crucial role in professional development, especially when working on large applications or collaborating with other developers.

  • Improve overall code readability and structure
  • Explain complex logic, calculations, or business rules
  • Temporarily disable code during testing or debugging
  • Serve as clear documentation for future maintenance

Types of JavaScript Comments

JavaScript provides two primary types of comments, each designed for different use cases depending on the amount of explanation or documentation required.

  • Single-line comments β€” used for brief explanations or inline notes
  • Multi-line comments β€” used for longer descriptions or detailed documentation

JavaScript Single-Line Comments

Single-line comments are used for short explanations and notes. They begin with two forward slashes (//). Anything written after // on the same line is ignored by JavaScript.

Syntax

// This is a single-line comment
            

Example 1: Using a Single-Line Comment

JavaScript Example

// Display a welcome message
console.log("Welcome to JavaScript!");
                    
Try it Yourself Β»

Example 2: Comment After a Statement

JavaScript Example

let number = 10; // store a numeric value
let result = number + 20; // add 20 to number
console.log(result); // output the result
                    
Try it Yourself Β»

JavaScript Multi-line Comments

Multi-line comments in JavaScript are used to write longer explanations or structured documentation that spans multiple lines. They are particularly useful for describing complex logic, application workflows, or providing detailed notes that help future developers understand the codebase.

A multi-line comment begins with /* and ends with */. Any text written between these symbols is completely ignored by the JavaScript engine during execution. Multi-line comments can be placed before a block of code, after it, or between individual statements without affecting program behavior.

Syntax

/* Your comment goes here */
    

Example: Using a Multi-line Comment in JavaScript

JavaScript Example

/*
  This block explains what the code does.
  It can span across multiple lines.
*/
console.log("Welcome to JavaScript learning!");
            
Try it Yourself Β»

Best Practices for Writing JavaScript Comments

Comments are a powerful tool for improving code understanding and long-term maintainability. However, when used incorrectly or excessively, they can reduce readability. Follow these best practices to write clean, professional, and effective JavaScript comments:

  • Write clear and concise comments: Focus on meaningful explanations and avoid unnecessary details.
  • Explain the reason, not the obvious: Good code should be self-explanatory; comments should explain intent or logic.
  • Keep comments accurate and up to date: Outdated comments can be more harmful than no comments.
  • Avoid excessive commenting: Too many comments can clutter the code and reduce readability.
  • Use descriptive naming conventions: Clear variable and function names often eliminate the need for extra comments.

When Should You Use JavaScript Comments?

Comments should be added intentionally and only where they provide real value. They are most effective in the following scenarios:

  • Explaining complex or non-obvious logic: Help other developers quickly understand intricate code behavior.
  • Documenting functions and variables: Clarify purpose, parameters, and expected outcomes.
  • Debugging and testing code: Temporarily disable sections of code without removing them.
  • Highlighting TODOs or future improvements: Leave clear notes for upcoming fixes or enhancements.

JavaScript Comments to Prevent Code Execution

In JavaScript, comments are not only used for documentation but can also serve as a practical tool to temporarily prevent code from executing. This technique is commonly used during debugging, testing new features, or experimenting with alternative logic without permanently deleting existing code.

Both single-line comments (//) and multi-line comments (/* ... */) can be applied to disable individual statements or entire sections of code while keeping them available for future use.

Example 1: Disabling a Single Line of Code

JavaScript Example

function showUserGreeting(userName) {
    let greeting = "Welcome, " + userName;

    // alert(greeting); // temporarily disabled for testing
    console.log(greeting);
}

showUserGreeting("Alex");
            
Try it Yourself Β»

Example 2: Disabling Multiple Lines of Code

JavaScript Example

function calculateOrderTotal(price, taxRate) {

    /*
    let taxAmount = price * taxRate;
    let totalWithTax = price + taxAmount;
    console.log("Total with tax:", totalWithTax);
    */

    let total = price;
    console.log("Total without tax:", total);
}

calculateOrderTotal(100, 0.1);
            
Try it Yourself Β»