JavaScript Comments

JavaScript Comments

JavaScript Comments

Comments in JavaScript explain code, make it more readable, and prevent execution of specific lines during debugging.

1️⃣ Single-Line Comments (//)

Use // for single-line comments. Everything after // on the same line is ignored by JavaScript.

// This is a single-line comment let x = 10; // Variable declaration console.log(x); // Output: 10

2️⃣ Multi-Line Comments (/* ... */)

Use /* ... */ for multi-line comments.

/* This is a multi-line comment. It can span across multiple lines. */ console.log("Hello, World!");

3️⃣ Commenting Out Code for Debugging

You can use comments to temporarily disable a part of your code.

// console.log("This line will not execute"); console.log("Only this line will execute.");

4️⃣ Documentation Comments (/** ... */)

Use /** ... */ for documentation-style comments. These are used by tools like JSDoc for generating documentation.

/** * Adds two numbers together. * @param {number} a - First number. * @param {number} b - Second number. * @returns {number} Sum of a and b. */ function add(a, b) { return a + b; }

5️⃣ Best Practices for Comments

Use comments only when necessary – Don't over-explain obvious code.
Keep comments short and clear – Be concise.
Update comments when code changes to avoid confusion.
Use comments for complex logic to help future developers.

🎯 Summary

Single-line comments//
Multi-line comments/* ... */
Documentation comments/** ... */ (for tools like JSDoc)
Use comments wisely for clarity and maintainability.

🚀 Need more details? Let me know! 😊

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close