JavaScript Comments

Learn JavaScript comments with syntax, examples, types, best practices, and real-world usage.

JavaScript Comments

Comments are lines in a program that are ignored by JavaScript. They are used to explain code, improve readability, and make programs easier to understand.

Comments are extremely useful when working on large projects because they help developers understand the purpose of the code.

Why Use Comments?

Comments make programs cleaner and easier to maintain.

  • Explain the purpose of code
  • Improve code readability
  • Help other developers understand the program
  • Make debugging easier
  • Temporarily disable code during testing

Types of JavaScript Comments

JavaScript supports two types of comments:

  • Single Line Comments
  • Multi Line Comments

Single Line Comments

Single line comments are used to write comments on one line. They start with double forward slashes //.

// This is a single line comment

console.log("Hello JavaScript");

Explanation

  • The line starting with // is ignored by JavaScript.
  • Only the console.log() statement gets executed.

Output

Hello JavaScript

Multiple Single Line Comments

// Program starts here

// Declaring variables

let name = "Ali";

// Printing output

console.log(name);

Multi Line Comments

Multi line comments are used when comments span across multiple lines. They start with /* and end with */.

/*

This is a
multi-line comment

*/

console.log("Welcome");

Explanation

  • Everything between /* and */ is ignored.
  • Useful for long explanations and documentation.

Using Comments to Explain Code

Comments are commonly used to explain complex logic.

// Store student marks

let marks = 90;

// Check if student passed

if(marks >= 40){

    console.log("Pass");

}

Using Comments for Debugging

Developers often comment out code while testing programs.

let a = 10;

let b = 20;

// console.log(a + b);

console.log(a * b);

The addition statement will not execute because it is commented out.

Good Comment Example

// Calculate total price after discount

let price = 1000;

let discount = 200;

let total = price - discount;

console.log(total);

Bad Comment Example

// Variable

let x = 10;

// Console

console.log(x);

Bad comments do not explain anything meaningful. Good comments should explain the purpose of the code.

Shortcut for Comments in VS Code

Most code editors provide keyboard shortcuts for comments.

  • Single Line Comment: Ctrl + /
  • Multi Line Comment: Shift + Alt + A

Nested Comments

JavaScript does not support nested multi-line comments.

/*

Outer comment

/* Inner comment */

*/

The above code may produce errors because nested multi-line comments are not allowed.

Real Life Example

// Student Information System

let studentName = "Rahul";

let marks = 85;

// Display student details

console.log(studentName);

console.log(marks);

Output

Rahul

85

Best Practices for Writing Comments

  • Write meaningful comments.
  • Keep comments short and clear.
  • Update comments when code changes.
  • Avoid unnecessary comments.
  • Use comments to explain logic, not obvious code.

Important Notes

  • Comments are ignored during execution.
  • Comments do not affect program output.
  • Good comments improve teamwork and maintenance.
  • Overusing comments can make code messy.

Summary

  • Comments are used to explain code.
  • JavaScript supports single line and multi line comments.
  • // is used for single line comments.
  • /* */ is used for multi line comments.
  • Comments help in debugging and maintenance.
  • Good comments improve code readability.