It is good programming practice to write comments with your blocks of code. Comments are simply notes to self, describing certain blocks of code- what is it that you have attempted to do with certain lines of code, how that fits into the whole document and any other explanations. Comments are ignored and not executed by browsers.
Giving headings to various parts of code using comments, makes looking at hundreds of lines less complicated. The comments are also useful if later on some other programmer works on your document, she can understand your document more easily if blocks of codes are explained or headings are used in comments.
You might recall that comments were also used in html. However they are much more needed in Javascript and other programming languages.
Javascript Single line comments
SYNTAX
| //comments |
Example

Javascript Multi line comments
SYNTAX
| /* comments comments */ |

NOTE: Observe that the comments part does not appear in the browser when page is loaded.
Javascript comments to temporary disable part of code
Javascript comments are also used to temporary disable some part of code in the whole document. If we wish to remove the code for some time, instead of deleting it we can comment it out, and later when wish to restore that functionality on the website, can just remove the comments /*.
/*
document.write("<p>This is an<strong> example</strong></p>");
*/
document.write("<p>This is an<strong> example</strong></p>");
*/
the above lines of code will not be executed now.
No comments:
Post a Comment