Javascript Comments

Comments are very useful for understanding complex code by explaining the complexity using plain language. In programming languages, comments are ignored by compiler, interpreter, translator.

Comments help programmers to read complex codes of a source file easily who did not write the source file. Or if you look back to a source file that you had written 6 months ago then it will be tricky to understand what you had done.

In this case, writing comments with plain language can help you to understand your code easily.

Comments also make source code reusable by thousands of programmers. Because all of them can make sense that what have done in the code by reading comments.

Javascript Comments

Javascript comments are ignored by web browsers.

In Javascript you can write comment instruction using two different ways:

  • Single-line comments
  • Multi-line comments

Single-line Comment

Single line comments are written after //I am a single-line comment

Example:

var mike = 20, bob = 17; // refes mike and bob's age, It is a cooment
// indiates single-line comment.
//single line cooment used to provide short information

For describing short information, using sing-line comment is very handy and reliable.

You can also use single-line comments for preventing a statement to be executed temporary for using an alternative one. And to reuse the statement again just remove the // sign to execute the statement.

Example:

//document.GetElementById("demo").innerHTML = "Hello, World!";
var myVar = querySelector(h1);
myVar.textContent("Hello, World!");

Multi-line Comment

Multiple-line comments are written inside /* I am a multiple line comment */

Example:

/* This below code blocks target the "h1" tag and replace
   replace the text with "This is my first heading"
   it is multi-line comment and described the function
   of the following code, and this statement is also
   considered as comments itself
*/
var myHeading.querySelector(h1)
myHeading.textContent = "This is my first heading";

Programmers often use multi-line comment to prevent execution a block of code beside writing informative comment and code explanation.

Example:

/*
var x = substraction(12, 9); 
function substraction(a, b) {
  return a - b;
} 
*/

In the above example, using multi-line comment you can ignore an entire block to be executed.

« Previous Next »