back JavaScript Statements

JavaScript statements are similar to their C/C++ counterparts but with some nice object oriented additions. Statements respresent the body of a JavaScript program by combining logic with expressions.

Embedded JavaScript implements a subset of the statements defined by the ECMAScript specification. The following statements are not supported: switch, while, do, break, continue, try/catch/finally/throw and with.

upStatement Overview


Statement
Syntax
Brief Description
;
;
Empty statement. Do nothing.
for
for (init; condition; increment)
statement
Standard for loop.
for (.. in
for (variable in object)
statement
Iterate over all properties in an object.
function
function name([arg1 [... , arg2])
{ statements }
Define a function.
if / else
if (expression)
statement
Conditionally execute a statement.
return
return [expression];
Return a value from a function.
var
var identifier [ = value ] [... , identifier [ = value]];
Declare and initialize variables.

upfor

The for statement provides the basic looping construct for EJS. Similar to the C for loop it implements an initialization, conditional and increment phases of the statement. The for statement also provides a for / in construct that is described below.

for (initialization; conditional; increment)
statement;

For example:

for (var i = 0; i < 10; i++) {
print("i is " + i);
}

You can put any expression or statement in the initialization or increment sections. In this example, we simply define a variable i and initialize it to zero in the initialization section and increment it each time after the statement body is executed. The conditional expression must evaluate to a boolean value which if true allows the statement body to be executed. The increment expression is evaluated after the statement body is executed and before the conditional is retested.

upfor .. in

The for statement has a powerful variant that allows you to iterate over all the properties in an object. It uses the following syntax:

for (variable in object)
statement;

This statement will execute the statement body for each property in the object. Each time, the variable will be set to the name of the next property in the object. The order of the walk through all the properties is undefined. NOTE: it will not be set to the property value, but will be set to the property name. For example:

for (var v in customer) {
println("customer." + v + " = " + customer[v]);
}

This will print "customer.propertyName = value" for each property defined in the customer object.

upfunction

The function statement defines a new global function according to the syntax:

function name([arg1 [... , arg2]) {
statements
}

The function name must be an identifier and not a reserved word and is followed by an optional list of arguments. Between braces, a set of statements define the function body. For example:

function min(arg1, arg2) {
if (arg1 < arg2) {
return arg1;
} else {
return arg2;
}
}

Function declarations can also be nested, i.e. a function may be defined in the statement body within an outer function. In this manner, the inner function will only be visible within the scope of the outer function.

When the function is invoked a new local variable store is created so that any variables declared and used in the function will be private to the function. Functions invoke other functions and each function will have its own local variables. If a variable is assigned to without using the var statement, the variable will be created in the global variable store.

upif / else

The if statement is the primary conditional execution ability with JavaScript. It takes the form:

if (expression) 
statement
[ else statement ]

The expression is evaulated and if true, the first statement is executed. If an else phrase is added and the expression evaluates to false, then the else statement will be executed.

Statements may be grouped using braces. For example:

if (i < j) {
println("i is " + i);
println("j is " + j);
} else {
// Do something
}

The conditional expression may be a compound conditional expression. For example:

i = 0;
j = 1;
if (i < j || j != 0 || getToday() == "sunday") {
// Do something
}

EJS uses lazy evalation where if the first expression (i < j) is true, then the following expressions will not be evaulated. In the example above, getToday() will not be called as i is less than j.

Switch

You can chain if / else statements together to implement a switch capability. For example:

if (today == "monday") {
    // Do something
} else if (today == "tuesday") {
   ...
} else {
    // Default
}

upreturn

The return statement is used to supply a retun value inside a function. The return statement may appear without an expression to cause the function's execution to terminate and return.

A return expression may be a simple value or it may be an object reference. For example:

function myFunc(x) 
{
    if (x == 0) {
        return null;
    }
    return new MyObj(x);
}

upvar

The var statement declares variables and initializes their values. Although not strictly required by the language, as variables will be declared automatically by simply assigning to them, it is good practice to always use var declarations for all variables. The var statement takes the form:

var identifier [ = value ] [... , identifier [ = value]];

For example:

var x;
var y = 4;
var a, b = 2, c = "sunny day";

If an initializer value is not defined, the identifier will be set to the undefined value.

If the var statement is used within a function, the variable is defined in the local variable store. If it is used outside a function, the variable is defined on the global store. If a variable is assigned to without a var statement, then the variable is created on the global store. For example:

x = 2;

function myFunc()
{
x = 7;
}

println("x is " + x);

This code snippit will print "x is 7".



© Mbedthis Software LLC, 2003-2204. All rights reserved. Mbedthis is a trademark of Mbedthis Software LLC.