JavaScript Comments
JavaScript comments are notes in your code that the computer ignores. They help explain your code, remind you why you wrote it, or guide teammatesβlike sticky notes for programmers.
Why Use Comments?
-
Explain what your code does (e.g., βCalculate total priceβ).
- Remind yourself of important details (e.g., βFix bug laterβ).
- Temporarily disable code while testing.
- Make code professional and team-friendly (a must-have skill in 2025).
Types of JavaScript Comments
1. Single-Line Comments (//)
A single-line comment starts with //. Everything after it on the same line is ignored.
// This prints a greeting
console.log("Hello, World!");
// Disable a line temporarily
// console.log("This wonβt run");
console.log("Code still works!");
2. Multi-Line Comments (/* ... */)
A multi-line comment starts with /* and ends with */.
Everything between is ignoredβeven across multiple lines.
/* This block of code
calculates total price with tax */
let price = 100;
let tax = price * 0.1;
console.log(price + tax);
/* Temporarily disable multiple lines
let x = 10;
console.log(x);
*/
console.log("Only this runs!");Β
