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. Statement Overview
forThe 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) For example: for (var i = 0; i < 10; 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. for .. inThe 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) 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) { This will print "customer.propertyName = value" for each property defined in the customer object. functionThe function statement defines a new global function according to the syntax:function name([arg1 [... , arg2]) { 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) { 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. if / elseThe if statement is the primary conditional execution ability with JavaScript. It takes the form:if (expression) 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) { The conditional expression may be a compound conditional expression. For example: i = 0; 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. SwitchYou can chain if / else statements together to implement a switch capability. For example:if (today == "monday") { returnThe 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) varThe 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; 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; This code snippit will print "x is 7". |