Index: > A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Business Industries Finance Tax

Home > JavaScript


First Prev [ 1 2 3 4 5 ] Next Last

5.4 Control structures

5.4.1 If ... else

if (condition) { statements } [else { statemesnts }]

5.4.2 While loop

while (condition) { statements }

5.4.3 Do ... while

do { statements } while (condition);

5.4.4 For loop

for ([initial-expression]; [condition]; [increment-expression]) { statements }

5.4.5 For ... in loop

This loop goes through all properties of an object (or elements of an array).

for (variable in object) { statement }

5.4.6 Switch statement

switch (expression) { case label1 : statements; break; case label2 : statements; break; default : statements; }

5.5 Functions

A function is a block with a (possibly empty) argument list that is normally given a name. A function may give back a return value.

function(arg1, arg2, arg3) { statements; return expression; }

Example: Euclid's original algorithm of finding the greatest common divisor. (This is a geometrical solution which subtracts the longer segment from the shorter):

function gcd(segmentA, segmentB) { while(segmentA!=segmentB) if(segmentA>segmentB) segmentA -= segmentB; else segmentB -= segmentA; return(segmentA); }

The number of arguments given when calling a function must not necessarily accord to the number of arguments in the function definition. Within the function the arguments may as well be accessed through the arguments array.

Every function is an instance of Function, a type of base object. Functions can be created and assigned like any other objects:

var myFunc1 = new Function("alert('Hello')"); var myFunc2 = myFunc1; myFunc2();

results in the display:

Hello

5.6 User interaction

Most interaction with the user is done by using HTML form s which can be accessed through the HTML DOM. However there are as well some very simple means of communicating with the user:

5.7 Events

Text elements may be the source of various events which can cause an action if a ECMAScript event handler is registered. In HTML these event handler functions are often defined as anonymous functions directly within the HTML tag.

List of events

1 Blur is when the object is deselected or something else is selected instead.
2 Focus is when the object is selected, usually in a form.

See also http://tech.irt.org/articles/js058/

5.8 Error handling

Newer versions of JavaScript (as used in Internet Explorer 5 and Netscape 6) include a try ... catch error handling statement.

The try ... catch ... finally statement catches exceptions resulting from an error or a throw statement . Its syntax is as follows:

try { // Statements in which exceptions might be thrown } catch(error) { // Statements that execute in the event of an exception } finally { // Statements that execute afterward either way }

Initially, the statements within the try block execute. If an exception is thrown, the script's control flow immediately transfers to the statements in the catch block, with the exception available as the error argument. Otherwise the catch block is skipped. Once the catch block finishes, or the try block finishes with no exceptions thrown, the statements in the finally block execute. This figure summarizes the operation of a try...catch...finally statement:

Here's a script that shows try ... catch ... finally in action step by step.

try { statements } catch (err) { // handle error } finally { statements }

The finally part may be omitted:

try { statements } catch (err) { // handle error }



Non User