Home » Online Computer Education » Learn Java Script, Adding Comments for Clarity, Lesson No. 21

Learn Java Script, Adding Comments for Clarity, Lesson No. 21

 

 
Comment statements have no effect on the logical performance of the script. However, they can be used to make scripts easier to understand by providing you with a means of internally documenting your scripts. You can place a comment in your scripts in either of two ways. To place a single comment line within the body of your script, type // followed by your comment, as shown here:

//This is an example of a script comment
You can also add a comment at the end of script statements as demonstrated here.

document.write(“Hello World”); //Write a message to the current
Window
If one line isn’t enough space for you to write out a complete comment, you may always add additional comments as demonstrated below.

//The following statement writes a message in the currently
open browser window
//using the document object’s write4() method.
document.write(“Hello World”);
Alternatively, you may create multiline comments by placing them inside the /* and */ characters as demonstrated in the following example.

/* The following statement writes a message in the currently
open browser window
using the document object’s write4() method. */
document.write(“Hello World”);
Comments are not always for other people; they are for you as well. For example, you might have a large script, several pages long, that allows customers to fill in a form and place orders for some type of product. If the tax rate changes, you’ll have to find the portion of the script that does this calculation and change it. If you had added a comment that identifies the line of code where the tax calculation is performed as demonstrated below, it would make it easier to modify the script years later.

//Compute the amount of tax to apply to the order
totalOrder = totalOrder + (totalOrder * .05);
The following script provides an example of how you might document your own scripts:

<HTML>

<HEAD>
<TITLE>Script 2.11 – Comment Example</TITLE>
</HEAD>

<BODY>

<SCRIPT LANGUAGE=”JavaScript” TYPE=”Text/JavaScript”>
<!– Start hiding JavaScript statements

//Define a variable representing the cost of a customer
order
var totalOrder = 100;

//Compute the amount of tax to apply to the order
totalOrder = totalOrder + (totalOrder * .05); //Tax
rate = 5%

/*The following statement simply demonstrates the use
of the document statement to write
a message on the current browser window.*/
document.write(totalOrder);

// End hiding JavaScript statements –>
</SCRIPT>

</BODY>
</HTML>

About

The main objective of this website is to provide quality study material to all students (from 1st to 12th class of any board) irrespective of their background as our motto is “Education for Everyone”. It is also a very good platform for teachers who want to share their valuable knowledge.

Leave a Reply

Your email address will not be published. Required fields are marked *