JavaScript

coding learning websites codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

JS Comments


📘 JavaScript Comments – Explain Your Code Clearly

Comments in JavaScript are lines that are not executed by the browser. They are meant to:

  • Make code easier to understand

  • Explain what a particular part of the code does

  • Temporarily disable a part of the code

JavaScript supports two types of comments:


🔹 1. Single-Line Comments

Use // to add a single-line comment.

// This is a single-line comment
let x = 5; // Declaring a variable

You can place it above a line or at the end of a line.


🔹 2. Multi-Line Comments

Use /* ... */ for multi-line comments. These are helpful when you want to comment out multiple lines of code or write detailed explanations.

/*
This is a multi-line comment.
It can span multiple lines.
*/
let y = 10;

🔸 Why Use Comments?
  • Documentation: Explain complex logic

  • Debugging: Temporarily remove parts of code

  • Collaboration: Help team members understand your code

  • Clarity: Make your code cleaner and easier to maintain


🔸 Commenting Out Code

You can use comments to disable code temporarily without deleting it.

// alert("This line is disabled temporarily");
console.log("This line runs");

Practice Questions

Q1. How do you write a single-line comment in JavaScript above a variable declaration?

Q2. How do you write a single-line comment at the end of a code line that declares a variable age with value 25?

Q3. How can you write a multi-line comment that explains what a function does?

Q4. How do you comment out a single JavaScript statement that displays an alert?

Q5. How do you write a comment that spans multiple lines to describe the logic of a conditional statement?

Q6. How do you use a single-line comment to disable a console.log() statement?

Q7. What is the correct way to use a multi-line comment to comment out two lines of code at once?

Q8. How do you place a comment in JavaScript inside a for loop to explain each iteration?

Q9. How do you write a block of code and add comments above each line to explain what it does?

Q10. How do you temporarily disable an entire function using multi-line comments?


JavaScript

online coding class codepractice

JS Basics

JS Variables & Operators

JS Data Types & Conversion

JS Numbers & Math

JS Strings

JS Dates

JS Arrays

JS Control Flow

JS Loops & Iteration

JS Functions

JS Objects

JS Classes & Modules

JS Async Programming

JS Advanced

JS HTML DOM

JS BOM (Browser Object Model)

JS Web APIs

JS AJAX

JS JSON

JS Graphics & Charts

Go Back Top