Comments explain JavaScript code, increase maintainability, enable IDE IntelliSense, and prevent execution during debugging. The JavaScript engine completely ignores comments during parsing and execution.
flowchart TD
Comments["JavaScript Comments"] --> Single["Single-Line: // Comment"]
Comments --> Multi["Multi-Line: /* ... */"]
Comments --> JSDoc["JSDoc Annotations: /** ... */"]
JSDoc --> TypeDoc["Provides Type Safety & Autocompletion in IDEs"]
| Type | Syntax | Best Use Case |
|---|---|---|
| Single-Line | // Note here |
Quick inline explanations, disabling a single line of code. |
| Multi-Line | `/* Line 1 | |
| Line 2 */` | Multi-paragraph explanation, temporarily disabling code blocks. | |
| JSDoc | /** @param {type} name */ |
Annotating function parameters, return types, and API descriptions. |
// ==========================================
// Single-Line Comment: Configuration Setup
// ==========================================
const API_TIMEOUT = 5000; // Timeout in milliseconds
/*
* Multi-Line Comment:
* The following section handles caching policy calculation.
* Ensures local storage is checked prior to making network calls.
*/
/**
* Calculates total invoice price including tax and discount.
*
* @param {number} basePrice - Base item price before adjustments.
* @param {number} taxRate - Tax percentage expressed as decimal (e.g., 0.15 for 15%).
* @param {number} [discount=0] - Optional discount amount to subtract.
* @returns {number} Final calculated price rounded to two decimal places.
*/
function calculateInvoiceTotal(basePrice, taxRate, discount = 0) {
if (basePrice <= 0) return 0;
const taxableAmount = basePrice - discount;
const total = taxableAmount + (taxableAmount * taxRate);
return Number(total.toFixed(2));
}
// Example Execution
const finalPrice = calculateInvoiceTotal(100, 0.15, 10);
console.log(`Calculated Invoice Total: \$${finalPrice}`);
let x = 5; // assigns 5 to x). Comment on business logic decisions and non-obvious algorithms.Write a JSDoc block for a function fetchUserData(userId) that accepts a numeric userId and returns a Promise resolving to an Object.
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.