What are comments?

To explain this the best, we will consider the following example.

Suppose a programmer made the following code two months before:
var a, rem, newStr;
newStr = "";
function myFunc(str) {
    for (a = 0; a < str.length; a++) {
        rem = a % 2;
        if (rem == 0) {
            newStr += str.charAt(a).toUpperCase();
        } else {
            newStr += str.charAt(a).toLowerCase();
        }
    }
    return newStr;
}
Now today he thinks of making some changes to the code above. So he opens up the file containing the code, goes on to make those changes but he stops and gazes over the code for some time. Why?

This is because he just can't understand what he himself wrote two months ago. He couldn't understand what is the statement a % 2 doing or what is rem == 0 being used for.

After a long time span of examining the code from the very start he finally understands the code and goes to make the changes. But does this happen always? Or is it the programmer's lack of understanding that he can't remember his own code? Is there a work around for this problem?

To answer these questions, comments are enough.
Comments, in programming, are statements ignored by the program interpreter or compiler. They are useful to programmers not to computers.

Writing Comments

You can write comments in JS in two ways:
var x = 10;

//var x = 15

var text = "Hello";

/*var text = "Hello! How are you doing programmer?"
See I am still not being executed */
  1. Single-line comment. The comment is only a single line following the // sign. If a new line is started (by pressing enter) and then some text is entered, the text wouldn't be considered as part of the comment
  2. Multi-line comment. The comment is everything between /* and */signs.
Be careful to put the */ sign after you have completed your multi-line comment or else any code following it would be considered as part of the comment or in some cases your overall JS code would fail to execute.

Benefits of using them

Now that we have introduced comments to you let's look at the very first example on this page again, but this time with comments:
var a, rem, newStr;
newStr = "";
function myFunc(str) {
   for (a = 0; a < str.length; a++) {
      rem = a % 2; 
      //rem stands for remainder 
      if (rem == 0) {
         /*
         rem == 0 checks if the remainder is 0
         TRUE means the character is at an even position
         */ 
         newStr += str.charAt(a).toUpperCase();
      } else {
         newStr += str.charAt(a).toLowerCase();
      }
   }
   return newStr;
}
If the programmer had written comments like this in his code two months before he would've saved alot of time that he wasted on just understanding the code all over again. So with comments:
  1. Programmers can easily recall their code without having to think over it for hours.
  2. Other people can also easily understand else's code without having to mentally visualise each and every statement.
  3. Complex programs can be well-structured by making long comments and naming them what an upcoming function does.