JavaScript Language Overview

 <  Day Day Up  >  


Readers familiar with programming should be able to inspect an existing JavaScript program with little trouble. A quick overview of the language to orient a reader familiar with programming is presented here. However, readers new to programming or looking for a detailed explanation are encouraged to learn JavaScript from any of the numerous online tutorials or books available, and might want to skip directly to the section "Common Scripts" to find a few useful copy-pasteable scripts.

As a programming language, JavaScript itself is not terribly difficult to learn. It shares syntax similarities with C, Perl, and Java and has only a few commands. As a language, it has only a few basic types: numbers such as 3, -45, 56.78; strings such as "Hello" and "Thomas Powell"; and the Boolean values true and false. The language also supports a few more complex data types such as arrays and objects that should be familiar to anyone who has programmed before.

Variables in JavaScript can be declared at any time. For example,

 var x = 5; 

would set a variable named x to a number and

 var today="Wednesday"; 

sets the variable today to the string "Wednesday". As a loosely typed language, it is possible to set variables to other types at any time, so a statement such as

 today = x; 

is perfectly legal and just changes the value of today to a number value of 5.

Note  

Although loosely typed languages such as JavaScript ease a burden on the programmer for keeping track of what type of data is variable, they also tend to introduce significant run time errors as a result of sloppy programming.

JavaScript is a case-sensitive language, so invoking the built-in alert method with a call such as alert('hello'); is okay, whereas Alert('hello'); is not. Note that most objects, properties, and methods in JavaScript should initially be lowercase with other words in the string capitalized. For example, alert() is all lowercase but document.lastModified does have the second part of the property initially capitalized. This follows the casing scheme found in many other languages. Remember that JavaScript is case sensitive while HTML is not. Of course, with the rise of XHTML, you will always want to use lowercase in HTML.

Statements in JavaScript are terminated with semicolons (;) or the return character, so

 alert("hi"); alert("there"); 

is equivalent to

 alert("hi") alert("there") 

However, if you remove the return in the second example, an error will occur, whereas putting two statements on the same line with semicolons between, as in

 alert("hi"); alert("there"); 

is perfectly fine.

JavaScript has a simple set of operators including basic arithmetic (+,-, /, *), Boolean comparisons (>, <, >=, <=, !, and = = ), string operators, and so on.

The statements of the language include conditional statements using an if - else syntax. Consider the following script, which alerts the user based on the value of the variable x .

 x=5; if (x > 4)   alert('Greater than 4'); else    alert('Less than or equal to 4'); 

More complex types of conditions also can be handled using a switch statement, whose syntax is similar to the C programming language. For example, rather than using a multitude of if statements, you could use a single switch with multiple case statements, as shown here:

 var x=3; switch (x)  {   case 1: alert('x is 1');           break;   case 2: alert('x is 2');           break;   case 3: alert('x is 3');           break;   case 4: alert('x is 4');           break;   default: alert('x is not 1, 2, 3 or 4'); } 

In the previous example, the value of x would determine which message was printed by comparing the value of the variable to the various case statements. If no match were found, the default statement would be executed. The break statement is also used commonly within switch to exit the statement once the appropriate choice is found. However, the break statement's use is also commonly associated with loops , which are discussed next .

Loops are primarily specified with while or for . Consider the short script here that alerts the user three times:

 x=1; while (x < 4) {  alert(x);  x++; } 

This also could be written as a for loop like so:

 for (var x = 1; x < 4; x++) {      alert(x); } 

Like most modern program languages, it is possible to abort a loop or modify the number of loop iterations using the break and continue statements.

The language also supports the use of functions. For example, you could define a function called sayHi that you use over and over in the document:

 function sayHi( ) {  alert('hi'); } 

This function could be called at any time with a simple call such as sayHi( ) . Good programming practice suggests that developers try to encapsulate commonly used code in functions.

Tip  

For more information on core JavaScript, visit Netscape's developer site at http://developer.netscape.com/. Information about Microsoft's implementation of JavaScript, called JScript, can be found at http://msdn.microsoft.com/scripting/.

While I have glossed over a great deal of issues including regular expressions, weak typing, functions, variable scope, and so on, the point to take home is simply that the JavaScript language itself is relatively simple for an experienced programmer to learn. The key to doing anything terribly useful with the language, however, is figuring out how to access the built-in objects of the browser as well as the objects related to the markup of an HTML or XHTML document. It is, in fact, the relationship of markup to JavaScript objects, dubbed the Document Object Model, that is the key to understanding the true power of scripting.



 <  Day Day Up  >  


HTML & XHTML
HTML & XHTML: The Complete Reference (Osborne Complete Reference Series)
ISBN: 007222942X
EAN: 2147483647
Year: 2003
Pages: 252
Authors: Thomas Powell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net