learn2kode.in

JavaScript Comments

Comments in JavaScript are notes you write in your code to explain what it does. They are ignored by the JavaScript engine — meaning they do NOT affect your program.

Comments help you:

JavaScript supports two types of comments:

1. Single-Line Comments (//)

Used to write short notes on a single line.

Example
// This prints a message
console.log("Hello World");

You can also add a comment after a line of code:

Single-line comments are great for:

Example
// console.log("This line is disabled");
2. Multi-Line Comments (/ ... /)

Used when you want to write comments that span multiple lines.

Example
/*
 This is a multi-line comment.
 You can write detailed explanations here.
*/
console.log("Learn2Kode Tutorials");

Useful when:

Example (disable multiple lines):

/*
let x = 10;
console.log(x);
*/
Where Comments Are Useful
Explaining code logic
// Check if user is logged in
if (isLoggedIn) {
  showDashboard();
}
Describing functions
/* 
  Calculates total price after discount
  price: original price
  discount: discount percentage
*/
function getDiscount(price, discount) {
  return price - (price * discount / 100);
}
Adding reminders
// TODO: Add form validation here
Debugging
// console.log(userData); // remove after testing
Best Practices for JavaScript Comments
Bad Comment:
let x = 10; // set x to 10
Good Comment:
let x = 10; // default starting value
Summary Table
Comment Type Syntax Use Case
Single-line // comment Short notes, disable one line
Multi-line /* comment */ Long explanations, disable blocks