Statements

This section describes how the following statements are used in ECMAScript:

Declarations

This table describes the main declarations used in ECMAScript.

Statement

Description

Example

function

The function statement is used to declare a function.

<script>
  function cannon(model, mp, price)
  {
   this.model=model;
   this.mp=mp;
   this.price=price;
   }
</script>

var

The var statement is used to declare a variable.

<script>
  var camera = new cannon('s50',
  '4MP','550.00');
</script>

Notations

This table describes the main notations used in ECMAScript.

Statement

Description

Example

//

The // notation identifies a single-line comment.

//This is a single-line comment.

/* */

The /* */ notation identifies a multi-line comment.

/*This is an example of a multi-line comment.*/

Iteration and control flow

This table describes the iteration and control flow statements used in ECMAScript.

Statement

Description

Syntax

break

The break statement terminates the current iteration of a loop and returns control to the statement following the loop.

break[labelname];

case

The case statement identifies a case expression within a switch statement.

case expression;

continue

The continue statement terminates the current iteration of a loop and starts a new iteration.

continue[labelname];

default

The default statement identifies the default case within a switch statement.

default:

do/while

The do/while statement executes a loop condition at least once, until the loop condition evaluates to false.

do{
   code;
}while(condition);

empty

The semi colon ";" is used to designate an empty statement.

;

for

The for statement executes a loop condition a specified number of times.

for(initialize; test; increment){
   code;
}

for/in

The for/in statement iterates a declared variable over the properties of a specified object.

for(variable in object){
   code;
}

if/else

The if/else statement executes the block of code within the if statement if the initial condition is true. If the initial condition is false, however, the block of code within the else statement is executed.

if(condition){
   code;
}else{
   code;
}

label

The label statement is an identifier that can be used with the continue or break statements to indicate where the program continues execution.

label:statement;

return

The return statement returns the value of a function back to the calling statement.

return expression;

switch

The switch statement executes statements when the value of a specified expression matches the case label.

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

throw

The throw statement allows you to throw exceptions.

throw exception;

try/catch/
finally

The try/catch/finally statement is used for error handling.

try{
     code;
}
catch(exception){
     code;
}
finally{
     code;
}

while

The while statement executes the block of code within the while statement until the loop condition is false.

while(condition){
   code;
}

with

The with statement establishes the default object for a statement.

with(object){
   code;
}