JavaScript Comments

In this tutorial, you will learn how to comment in JavaScript code. JavaScript code that is ignored by the JavaScript engine when the code is executed.

It is used to explain code, make notes, or temporarily disable certain parts of the code.

Comments are helpful for both the developer writing the code and others who might read or maintain the code in the future.

There are two types of comments

Single-Line Comments

It is used for a single-line. It is used to add short notes or explanations on one line.

They start with // and extend to the end of the line.


// This is a single-line comment
let name = 'John'; // Variable name is assigned the value John

Multi-Line Comments

It is used for Multi-Line Comments. It is used for longer explanations or for commenting out multiple lines of code.

They start with /* and end with */


/*
This is a multi-line comment.
It can span multiple lines.
It is useful for longer descriptions or temporarily disabling blocks of code.
*/
let x = 10;

JavaScript Comments – Interview Questions

Q 1: What are comments in JavaScript?
Ans: Comments explain code and are ignored during execution.
Q 2: Single-line comment syntax?
Ans: // comment
Q 3: Multi-line comment syntax?
Ans: /* comment */
Q 4: Why use comments?
Ans: To improve code readability and maintenance.
Q 5: Can comments affect performance?
Ans: No, they are ignored by the interpreter.

JavaScript Comments – Objective Questions (MCQs)

Q1. Which symbol is used for single-line comments?






Q2. Multi-line comments start with ______.






Q3. Multi-line comments end with ______.






Q4. Comments are used to ______.






Q5. JavaScript comments are ignored by the ______.