JavaScript Comments & Delimiters

Master the art of writing clean, readable, and stable JavaScript by properly using comments and semicolons.

Lesson ProgressStep 1 of 8
// Calculate total score
let score = 100;
/*
Add bonus points if user is high-level.
*/
let bonus = 20;
0 EXP

Hello! Let's learn how to leave notes in our JavaScript code. These are called comments.

// Welcome to the JavaScript simulator

Single-Line Comments (`//`)

A single-line comment starts with `//`. The JavaScript engine ignores everything from the `//` to the end of the line.

They are perfect for short explanations, or for temporarily disabling a line of code (a process called "commenting out").

// This is a comment explaining the variable
let userName = "Sarah"; // This is an inline comment

// console.log(userName); // This line is "commented out"

System Check

Which syntax correctly creates a single-line comment?

Advanced Holo-Simulations

0 EXP

Log in to unlock these advanced training modules and test your skills.


Achievements

🗒️
Comment Connoisseur

Use both single-line and multi-line comments.

🛡️
Semicolon Sentinel

Correctly use semicolons to terminate statements.

✍️
JSDoc Pro

Write a valid JSDoc block for a function.

Mission: Document a Function

Complete the `add` function. Add a full JSDoc block (`@param`, `@returns`) and a single-line comment inside. Don't forget the semicolon!

A.D.A. Feedback:

> System integrity looks stable. Code is valid.

Challenge: Order the Code

Drag the elements into the correct logical order for documenting code.

/* A multi-line comment */
// A single-line comment
let a = 1;

Challenge: Complete the Syntax

Fill in the missing parts of the comments.

This is a single line.
This is a
multi-line comment.

Consult A.D.A.

Community Holo-Net

The Art of Readable Code: Comments & Semicolons

Writing code that a computer understands is only half the battle. Writing code that **humans** understand is the mark of a professional. In JavaScript, this battle is won with two simple tools: comments and semicolons. They are the keys to clarity and stability.

The 'Why', Not the 'What': A Philosophy of Commenting

A common mistake is writing comments that explain *what* the code is doing. Good code should be self-explanatory. Instead, your comments should explain **why** the code is doing it.

❌ Bad Comment (The 'What')

// Add 1 to i
i++;

This is redundant. The code already says this.

✔️ Good Comment (The 'Why')

// Offset by 1 to match the 1-based index
i++;

This explains the business logic or intent.

JSDoc: The Professional Standard

When you write a function or class, you're creating an API for other parts of your code (or other developers) to use. JSDoc is a formal syntax for multi-line comments that describes this API. Modern code editors read JSDoc to provide autocomplete and type-checking.

/**
 * Calculates the total price, including tax.
 *
 * @param {number} price - The base price of the item.
 * @param {number} [taxRate=0.07] - The tax rate (e.g., 0.07 for 7%).
 * @returns {number} The final price including tax.
 */
function calculateTotal(price, taxRate = 0.07) {
  // Ensure price is a positive number
  if (price <= 0) return 0;
  
  return price * (1 + taxRate);
}
  • @param Describes a parameter.
  • [taxRate=0.07] The brackets `[]` mean the parameter is optional, and `=0.07` shows its default value.
  • {number} Describes the expected data type.
  • @returns Describes what the function returns.

The Hidden Enemy: Automatic Semicolon Insertion (ASI)

JavaScript has a feature called Automatic Semicolon Insertion (ASI). If you forget a semicolon, the engine tries to guess where it should go. This "feature" is a notorious source of bugs. The most common error happens with the `return` keyword.

function getUser() {
  return  // <--- ASI inserts a semicolon here!
  {
    username: "alex"
  };
}

getUser(); // Returns undefined, NOT the object!

Because the `return` keyword is on its own line, ASI "helpfully" adds a semicolon after it, making your function return `undefined`. The object below it becomes "unreachable code."

Key Takeaway: Always use comments to explain the **why**. Use JSDoc for all functions. And **always** use semicolons to terminate your statements. Do not rely on ASI. This discipline will save you hours of debugging.

JS Comments & Syntax Glossary

Comment
Non-executable text within the code, ignored by the JavaScript engine. Used for documentation and human-readable notes.
Single-Line Comment (`//`)
Starts with `//` and comments out the rest of the line.
Multi-Line Comment (`/* ... */`)
Starts with `/*` and ends with `*/`. Comments out everything in between, even across multiple lines.
JSDoc
A formal documentation syntax, written inside `/** ... */` comments, used to describe functions, parameters, and return values.
Delimiter
A character that separates one piece of code from another. The semicolon is the primary statement delimiter in JavaScript.
Semicolon (`;`)
The character used to terminate a JavaScript statement.
Statement
A single instruction for the computer to execute, e.g., `let a = 1;`.
Automatic Semicolon Insertion (ASI)
A JavaScript feature that automatically inserts semicolons where it thinks they are missing. It is a common source of bugs and should not be relied upon.
Debugging
The process of finding and fixing errors ("bugs") in code. "Commenting out" code is a common debugging technique.

About the Author

Author's Avatar

TodoTutorial Team

Passionate developers and educators making programming accessible to everyone.

This article was written and reviewed by our team of web development experts, who have years of experience teaching JavaScript and building robust web applications.

Verification and Updates

Last reviewed: October 2025.

We strive to keep our content accurate and up-to-date. This tutorial is based on the latest ES2025 specifications and is periodically reviewed to reflect industry best practices.

External Resources

Found an error or have a suggestion? Contact us!