JavaScript

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

JavaScript

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?


JS Comments Quiz

Q1: Which symbol is used to write a single-line comment in JavaScript?

A. #
B. <!--
C. //
D. **

Q2: Which of the following is the correct syntax for a multi-line comment?

A. ## This is a comment ##
B. <!-- This is a comment -->
C. /* This is a comment */
D. ** This is a comment **

Q3: What will happen to a line of code that is commented out using //?

A. It will execute normally
B. It will be skipped by the browser
C. It will produce an error
D. It will be printed on screen

Q4: Which of the following is NOT a valid use of JavaScript comments?

A. Explaining code
B. Hiding code
C. Changing variable values
D. Disabling code

Q5: Which comment syntax would you use to explain a block of code with several lines?

A. <!-- ... -->
B. //
C. **
D. /* ... */

Q6: Where can single-line comments be placed in JavaScript?

A. Only at the beginning of the file
B. Only after code lines
C. Anywhere in the script
D. Only in the HTML head

Q7: What does this line do: // let x = 10;?

A. Declares a variable
B. Assigns a value
C. Ignores the code
D. Creates a function

Q8: What happens if you don’t close a multi-line comment?

A. The page crashes
B. An error occurs
C. The rest of the script is ignored
D. Nothing happens

Q9: How many lines can a multi-line comment span?

A. One line only
B. Exactly two lines
C. As many as needed
D. Only in loops

Q10: What is the primary purpose of JavaScript comments?

A. To speed up code execution
B. To help browsers understand code
C. To document and explain code
D. To change how variables work

Go Back Top