Web Development Tutorials




  
  

JavaScript Comments

Often you will want to include comments within your code to document the purpose and functioning of scripts. Comments are simply lines of text that describe or explain the script; they are not treated as executable code. They are useful to the programmer in helping to understand a script, especially when returning to edit the script after a lengthy period of time.

JavaScript comments come in two varieties. Single-line comments are designated by two slash characters "//" appearing before the commentary text. The comment can appear on a line by itself or on the same line as an executable statement as long as it follows the statement.

// The following function changes the text style of a clicked
// element whose identity is passed to this function.

function changeStyle(someTag)
{
    someTag.style.fontSize = "2em";     // Change the font size
    someTag.style.fontWeight = "bold";  // Change the boldness
    someTag.style.color = "red";        // Change the color
}

Figure 3.1 Two slashes start a single line comment.

An alternative for multiple-line comments is to use the characters /* at the beginning of the commentary text and the characters */ after the commentary text. This notation is most often used for multiple comment lines although it can appear on a single line of script following any statements on the line. You can freely intermix the two notation types.

/*------------------------------------------------------------
| The following function changes the text style of a clicked |
| element whose identity is passed to this function.         |
----------------------------------------------------------- */

function changeStyle(someTag)
{
  someTag.style.fontSize = "14pt";    // Change the font size
  someTag.style.fontWeight = "bold";  /* Change the boldness */
  someTag.style.color = "red";        // Change the color
}

Figure 3.2. Multilines comments use the /* and */ to enclose comments.

These tutorials are not complete coverage of the language, but they do provide a starting toolkit to code many common scripts that can make your Web pages interactive with users and dynamic in content and structure. The examples and illustrations are kept simple; however, they provide the foundational know-how that can be embellished as needed to provide visitors with nearly full control over their experiences with your Web site.


TOP | NEXT: Error Messages