Table of Contents

JavaScript Comments

Introduction

JavaScript comments are hints that a programmer can add to make their code easier to read and understand. They are completely ignored and are not executed.

Advantages of using Comments

  • JavaScript comments can also be used to prevent execution, when testing alternative code.
  • JavaScript comments can be used to explain JavaScript code, and to make it more readable.

Types of Comments

  1. Single Line Comments
  2. Single line comments start with //.
  3. Any text you type, between // and the end of the line will not be executed.
  4. Example :
  5. var x=20;
    // x = 40;

    console.log(x);
  6. Here,
  7. 20 will be displayed in the Console Tab because first we declared x and stored the value of 20 in it.
  8. then we have reassigned the value of x to 40. (but this line is commented and will not be executed).
  9. So, 20 will be displayed in the Console.
  10. Multi Line Comments

Multi-line comments start with /* and end with */.

Any text between /* and */ will be not be executed.

Example :

//variable declaration
var x = 10;

/*
var y = 20;
var z = 50;
x = y + z;
*/

console.log(x);

Here,

10 will be displayed in the Console Tab because first we declared x and stored the value of 10 in it.

then we have reassigned the value of x to y + z i.e. 50 + 20 (but this line is commented and will not be executed).

So, 10 will be displayed in the Console.

You can write anything inside comments, it will not be executed. As in first line I have written variable declaration in the commented line for reference that variable declaration starts from here.

Ask queries
Contact Us on Whatsapp
Hi, How Can We Help You?