o Repeat after me : Comments are important. Comments are important. Comments are important.
o Adding comments to your code is always good practice. As the complexity of your scripts grows, comments help you (and others) understand their structure when you come to view the code at a later date.
o A lot of code created quickly is said to be “write only” code, as it suffers from an inherent lack of structure or commenting. Debugging such code, or reusing it months later, becomes maddeningly impossible as you try to remember what a certain line was supposed to do, or why using certain values seems to stop your code from working.
o Comments can slow the loading of your page, however – many coders keep a “development” copy of their code fully commented for editing, and remove all comments from their code when they finally publish it.
o Single line comments begin with two forward-slash characters (//), and end with a new line:
// this is a comment
alert(“hello”); // so is this
<!– this is a comment
o Note two things: firstly, this use of the HTML comment format does not require a closing –> tag. Secondly, this is only a one line comment, unlike its use in HTML, which comments all lines until the closing comment tag.
o You can add multiple-line comments by enclosing the comment block between /* and */. For example:
/* a one line, “mult-line” comment */
o As well as adding narrative to your script, you can use comments to remove code from your pages without having to delete the code. For example:
// this was the old message
// alert(“Hello World”); // and this is the new message alert(“Hello everyone!”);
o This can be very useful if you are trying to track down an error in your code – you can “comment out” each suspect line in turn until you manage to get your code working again.
o Open your previous project file, and save it under the name chapter_7.html.
o Add the single line comment
This is my first comment
to the beginning of your script.
Add a multi-line comment to your script, replacing your previous single line comment. The multi-line comment should describe what your script does at present.