JavaScript comments are notes inside source code that the engine completely ignores at runtime. Developers use comments to explain non-obvious business rules, document function parameters, and temporarily disable lines during debugging.
JavaScript supports three comment formats depending on how much context you need to share:
//): Ignore everything from // to the end of that line. Ideal for brief notes or disabling one statement./* ... */): Ignore everything between /* and */ across one or more lines./** ... */): Structured comments that describe function arguments and return types for IDE tooltips and automated docs./**
* Calculates shipping cost based on weight in kilograms.
* @param {number} weightKg - Package weight in kg.
* @returns {number} Shipping fee in USD.
*/
function getShippingFee(weightKg) {
// Free shipping applies to light promotional samples under 0.5 kg
if (weightKg < 0.5) {
return 0;
}
/* Base rate is $4 plus $1.50 per extra kilogram.
Keep this formula aligned with billing-service v2. */
return 4 + Math.ceil(weightKg) * 1.5;
}
console.log("Fee for 2.2kg:", getShippingFee(2.2));
Good code explains what happens through clear variable and function names. Good comments explain why a weird workaround or business threshold exists. Avoid restating the obvious like // increment counter by 1 above counter++.
/* outer /* inner */ outer */ breaks because the first */ closes the entire comment block early.Comments have zero impact during execution, though large comments increase file download size. Production bundlers like Vite and Webpack strip all comments automatically when minifying code.
Add JSDoc blocks to shared utility functions, API helpers, and library exports. Editors like VS Code read JSDoc tags to show autocomplete hints without requiring TypeScript.
Yes, press Ctrl + / on Windows or Cmd + / on macOS in VS Code to toggle // comments on selected lines.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.