When we are doing a good programming and we are following ethics of programming we are good programmers. We know later maybe someone need to edit our code or will try to read our code and they need some hints and suggestions from us about our code. Well if you are writing your code today and after 1 week if you need to edit your code you will forget everything and you will have to read whole code again to get an understanding with your own code even . To make our code describing itself and to make our code so it can tell others what i am doing we give comments in programming languages. In JavaScript we have two different ways to give comments they are following.
One line comment JavaScript
If we are giving comment only to 1 line then there is very easy method in JavaScript like if you have declared a variable and now you want to name it then dont worry you can do it. Also if you are writing a while loop and on end you want to give a one line comment how will you give that?
This is the way how you can give one line comments in javaScript
[code lang=”js”]
<script type="text/javaSCript">
count = 10; //this is my counter and i am one line comment.
while(counter) {
//i am doing something but i am commented.
counter++;
}//counter loop ends here.
</script>
[/code]
The things after // double slash line is a comment and will not effect on code or will not do anything.
Commenting whole code block in JavaScript
Okay when you have a block of code and you do not need that now but you want to keep that for future use how will you take out that? Will you comment it line by line by one line commenting ? or will you comment the whole block with two lines. You can comment by starting /* and whole thing after that will be commented unless you write */ see a commented function below which will not do anything.
[code lang=”js”]
<script type="text/javaScript">
/*function i_add_num(first, second) {
sum = first+second;
return sum;
}*/
</script>
[/code]
The above code or function is unavailable cause its commented.