// single-line
Best for short notes above or beside one line of code.
Comments are notes for humans. They explain intent, warnings and context, but they should not repeat obvious code or hide dead code forever.
// Keep this delay aligned with the CSS transition. const animationDuration = 220; setTimeout(closePanel, animationDuration);
JavaScript Basics
JavaScript has single-line comments with // and block comments with /* ... */. Both are ignored when the code runs.
A useful comment gives context that the code cannot show by itself: a business rule, browser workaround, security reason or a warning about timing.
Bad comments are noise. If the code says total = price * count, a comment that says calculate total adds nothing. Clear code first, comments second.
Best for short notes above or beside one line of code.
Useful for longer notes, temporary section labels or documentation blocks.
Structured comments can document functions, parameters and return values.
Comments are shipped with source in many projects. Do not put passwords or private keys in comments.
Examples
// The API returns cents. Convert once at the boundary so UI code works with display credits. const amountInEuros = response.amountInCents / 100; // Keep focus inside the modal while it is open. trapFocus(modal);
// Set the display label const name = "User A"; // Add one count = count + 1; // old code maybe needed later // submitBrokenFormForever();
Rules that matter
JavaScript becomes much easier when you understand why a pattern exists. The syntax is only the surface; the real skill is choosing the right behavior for the interface.
Use comments for why a decision exists, not for what the syntax already says.
Put the note near the code it explains.
Wrong comments are worse than no comments because they mislead the next developer.
Use version control instead of leaving old code commented out forever.
Comments can leak in repositories, source maps, backups or shared snippets.
If a better variable name removes the need for a comment, rename the variable.
Production thinking
Comments are part of maintainability. They help future developers understand decisions that are not visible from syntax alone.
Comments are useful around focus management, ARIA updates and keyboard behavior because those decisions are easy to break accidentally.
Production teams should treat comments as documentation that must stay accurate. Outdated comments slow down debugging.
Comments do not help ranking. They help developers avoid breaking the JavaScript that supports navigation, content and interaction.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read the topic. Change the code, explain what happened and answer the questions in your own words.
Chapter checkpoint
Finish this chapter by turning the lessons into a small practical proof.
Build one HTML page with a button, a status message and an external JavaScript file.
Can you explain what part is HTML, what part is CSS and what part is JavaScript?