Chapter 05

JavaScript Comments

Comments are notes inside the code that explain the program. JavaScript does not execute comments.

What is JavaScript Comments?

  • A single-line comment starts with //.
  • A multi-line comment is written between /* and */.
  • Comments help developers understand and maintain code.
  • Comments are not normally displayed to users on the webpage.

Syntax

// Single-line comment

/*
   Multi-line comment
*/

Example

Try understanding this code before running it.
// Store the student's name
let name = 'Rahul';

/* Store the student's marks
   in a variable */
let marks = 85;

console.log(name);
console.log(marks);

Important Points

  • Use comments to explain useful or non-obvious parts of the code.
  • Keep comments accurate and up to date.
  • Never store passwords, API secrets, or other sensitive information in comments.
  • Comments are especially useful when working on team projects.

Common Mistakes

  • Forgetting to close a multi-line comment.
  • Adding unnecessary comments to every line.
  • Putting passwords or secret keys inside comments.

Real-World Use

Professional developers use comments to explain complex logic, important decisions, and sections of a project.

Practice Exercise

Practice: Comments
Add one single-line comment and one multi-line comment to your JavaScript program.

Ready to write code?

Open the JavaScript editor and run your code with live output.

Try It Yourself →