JavaScript Introduction
CIW Website Design Manager Course Section 2, Part A1, Chapter 1
Java v JavaScript
I shall keep this short and sweet, the course notes provide a nice table with all the differences between Java and JavaScript, I won't include this here. Suffice to say that Java is an object-oriented, compiled language, where JavaScript is an object-based, script language.
JavaScript was originally known as LiveScript.
JavaScript Applications
JavaScript code may be embedded in an HTML document or it may be in a separate file which is referenced from the HTML document. The syntax for each is shown below:
| <script type="text/javascript" language="JavaScript" src="site.js"> </script> |
This is the code to include an external JavaScript file. In this case the file is named site.js. The main advantage of using an external file is that the code can be shared by many Web pages allowing much easier modifications in the future.
| <SCRIPT LANGUAGE="JavaScript"> <!-- JavaScript code is placed here. /* JavaScript comments, such as this, allow the code to be documented */ //--> </SCRIPT> |
The structure of embedded code is much the same. Note the HTML comment characters, which allow earlier browsers to skip the JavaScript code which they cannot support.
Comment Indicators
We have already looked, briefly, at comments in the code sample above. The comment in that sample is a multiple line comment indicator. A single line indicator is simply //.
// This is a single line comment
/* And this is a longer section of comment
that will span multiple lines */
It is good practice to include comments in all code that you develop, even if it is only you that will be using the code. It is surprising how quickly you forget what the code was intended to do, when you revisit it several weeks or months later.
Comments are even more important if the code is likely to be viewed by other program developers, as they will not, necessarily, know your objectives from the code itself.

