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:
Used to write short notes on a single line.
// This prints a message
console.log("Hello World");
You can also add a comment after a line of code:
let age = 25; // user age
Single-line comments are great for:
// console.log("This line is disabled");
Used when you want to write comments that span multiple lines.
/*
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);
*/
// Check if user is logged in
if (isLoggedIn) {
showDashboard();
}
/*
Calculates total price after discount
price: original price
discount: discount percentage
*/
function getDiscount(price, discount) {
return price - (price * discount / 100);
}
// TODO: Add form validation here
// console.log(userData); // remove after testing
let x = 10; // set x to 10
let x = 10; // default starting value
| Comment Type | Syntax | Use Case |
|---|---|---|
| Single-line | // comment |
Short notes, disable one line |
| Multi-line | /* comment */ |
Long explanations, disable blocks |