Lesson 7 of 30 – JavaScript Comments
23%

JavaScript Comments

JavaScript comments are used to explain code and make programs easier to understand. They are ignored by the JavaScript engine and are not executed.

Note: Comments help developers understand, maintain, and debug code.
Why Use Comments?

Comments are useful for:

  • Explaining code.
  • Improving readability.
  • Debugging programs.
  • Temporarily disabling code.
  • Helping other programmers understand your work.
Single Line Comments

Single-line comments start with //.

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

Example:
// Change paragraph
document.getElementById("p1").innerHTML = "This is paragraph";

// Change heading
document.getElementById("h1").innerHTML = "This is heading";
Output:

The comments are ignored, but the JavaScript statements execute normally.

Multi-Line Comments

Multi-line comments begin with /* and end with */.

They can span several lines.

Example:
/*
This is a multi-line comment.
Soopro Pathshala provides
online and offline training.
*/

JavaScript ignores everything inside the comment block.

Using Comments for Testing

Comments can temporarily disable code while testing.

Example:
var x = 10;
// var y = 20;
var z = 30;

The commented line will not execute.

Types of JavaScript Comments
Type Symbol Use
Single-line // Short explanations.
Multi-line /* */ Long descriptions or large code blocks.
Best Practices for Comments
1. Keep comments short and meaningful.
  • Avoid unnecessary details.
  • Write comments that are easy to understand.

2. Explain WHY, not WHAT.
  • Good code often explains what it does.
  • Comments should explain the reason behind the code.
Comment Complex Logic

Use comments when the program contains difficult calculations or special logic.

Example:
// Calculate final percentage after bonus marks
total = marks + bonus;

This helps other programmers understand the purpose of the code.

Avoid Excessive Comments

Too many comments can make code difficult to read.

Bad Example:
// Create variable x
var x = 10;
Better Example:
// Store default passing marks
var passingMarks = 33;
Advantages of Comments
  • Improve readability.
  • Make debugging easier.
  • Help teamwork.
  • Document complex code.
  • Allow temporary disabling of code.
  • Reduce maintenance effort.
Real-Life Example
// Get student marks
var marks = 85;

// Check pass or fail
if(marks >= 33){
document.write("Pass");
}
else{
document.write("Fail");
}

The comments explain the purpose of each section of the program.

Key Points
  • Comments make JavaScript code easier to understand.
  • JavaScript ignores comments during execution.
  • Single-line comments start with //.
  • Multi-line comments start with /* and end with */.
  • Comments help during debugging.
  • They can temporarily disable code.
  • Use comments to explain complex logic.
  • Avoid unnecessary comments.
Quick Revision
Comment Type Symbol
Single-line //
Multi-line /* ... */
Best Practices
  • Keep comments short and meaningful.
  • Explain why the code exists.
  • Comment difficult logic.
  • Use meaningful variable names.
  • Avoid obvious comments.
  • Remove outdated comments.

🧠 Quick Quiz

Which symbol is used for a single-line comment in JavaScript?