JavaScript Comments

Commenting is the most important part of any programming language. Have you ever written a program or a script in the past and if you try to see that code now, will you be able to remember what is going on in the code?

Probably you forgot to do what all other programers forget to do, and that is to write the meaning of full code which you have written.

When you are writing any code, may be you are writing some complex logic, some important formula which is confusing, and that is the perfact situation/time to write some meaningful comment for that code, so later when you try to understand or modify that code you can easily recognise the purpose of that code and why you have written that.

In other case it's not always necessary that only you will modify your code later, it may happen that later someone else may modify the code which you have written, so that is very usefull to know for them that why you have written that code. Another thing for debugging purpose also you can put some unimportant code into comment to skip the execution.

In JavaScript there are 2 types of commenting allowed, single line comment and multiline comment

Single line comment

If you want to create a single line comment in JavaScript then you can use two forward slashes. What is the text written on the right side of two forward slashes will be ignored by javascript interpreter. Whenever we want to write a small notes for our code then this single line of code will be used. For example

// It will change blog title
document.getElementById("blogtitle").innerHTML = "JavaScript Comments";

// It will change blog post
document.getElementById("blogpost").innerHTML = "We will see how to write commented code in javascript";

// in above code snipper whatever line starts with '//' will be ignored 
// by javascript interpreter, means it will not be executed.sssss

Multiline comment

However single line comment is very useful, sometime it's necessary to write bigger comment to explaing the logic of the code we have written to perform complex calcuation, in this situation multiline comment is useful. It will start with /* and ends with */. Whatever code is written between /* and */ will be ignored by JavaScript interpreter. For example

This code will be used value from setting table and and mutliply by with
the transaction table so the result can be different for each salesman.
var salesmanPercentage = "Some value from setting table based on candidateid"; 
var salesmanSale = "Salesman sale from the transaction table";
var salesmanCommission = salesmanPercentage * salesmanSale;


← Previous

Next →