SPS Home    >   Dgreath    >   JavaScript    >   Statements

JAVASCRIPT STATEMENTS

The following information describes JavaScript's current statement implementation.
  • Loop constructs (do...while, for, for...in, and while)
  • Loop control (break and continue)
  • Program control (switch...case)
  • Conditional constructs (if, if...else)
  • Conditional functions (function, return)
  • Error handling (throw, try, catch)
  • Declaration (var, with)

These should work on most browser implementations.

For further information, see articles about  [label], (CONDITION), and {BLOCK} expressions.


break
Stops the currently executing do...while, for, for...in or while loop and transfers program control to the statement following the terminated loop. The optional label allows the specification of which loop is to broken in the case of nested loops. Break is also used to end case blocks used in switch statements.
Syntax: break [label]
See also: continue
Example of a for loop with a labeled break:
myLoop: for ( i = 0 ; i < 9 ; i++ )
{
  document.write( i );
  if ( i == 8)
  {
    break myLoop
  }
  document.write(" + ")
}
case
Defines a choice in a switch statement. If the value of label matches the target expression, the statement block will execute. All following case/default statements will also execute unless a break is encountered.
Syntax: case label: {BLOCK }
For example of usage see switch.
case...break
Defines a choice in a switch statement. If the value of label matches the target expression, the statement block will execute. When the break is encountered, program execution then continues with the next statement after the switch. 
Syntax: case label: { expr1 ; expr2 ; break }
For example of usage see switch.
catch
The error handler function called by a throw in a try statement.  The parameter "e" is the value of the exception thrown by the throw statement in the try block. After the exception is handled, program execution continues to the finally block, if present, or, if not, to the next statement.
Syntax: catch(e) {BLOCK }
See also throw.
For example of usage see try.
continue
Restarts a currently stopped do...while, for, for...in, or while loop and continues execution of the loop with the next iteration.
Syntax: continue [label]
See also: break
Example of a for loop with a labeled continue:
myLoop: for ( i = 0 ; i < 9 ; i++ )
{
  document.write( i );
  if ( i == 8)
  {
    continue myLoop
  }
  document.write(" + ")
}
default
Defines the default choice in a switch statement. The default statement must be the last in the switch construct. Its statement block will execute if no preceding case or case...break statement has matched the target expression. Its statement block will not execute if any preceding case...break statement has matched the target expression.
Syntax: default: {BLOCK}
For example of usage see switch.
do...while
A loop constructor that executes the specified statements until the test condition evaluates to false. Statements will execute at leas at once. The do...while loop is used when the conditions for loop termination are not known prior to execution of the loop and are to be determined inside the loop execution. The optional break statement will cause loop execution to halt and passes control to the next statement following the loop.  The optional continue statement will halt loop execution at that point and passes control to the condition expression.
Syntax: [label:] do {BLOCK} while (CONDITION)
See also: while and for
See break and continue for more information about the use of label.
Example:
var x = 0;
do
{
  document.write( x );
  x++
}
while (x < 9);
finally
Contains the portion of executable code for a try statement that is not monitored for thrown exceptions.
Syntax: finally {BLOCK }
For example of usage see try.
for
The basic loop constructor. It creates a loop that consists of three optional expressions, enclosed in parentheses and separated by semicolons, followed by a block of statements to be executed in the loop. For loops are used when the conditions for loop termination are known prior to execution of the loop.The optional label allows the specification of which loop is to broken in the case of nested loops
Syntax: [label:] for ([initialExpr]; [CONDITION]; [incrementExpr]){BLOCK}
See also: do...while and while
See break and continue for more information about the use of label.
Example:
for ( i = 0 ; i < 9 ; i++ )
{
  document.write( i );
  document.write(" + ")
}
for...in
A special case loop that iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements. The optional break statement will cause loop execution to halt and passes control to the next statement following the loop.  The optional continue statement will halt loop execution at that point and causes the loop to begin processing the next property of the object. The optional label allows the specification of which loop is to broken in the case of nested loops
Syntax: [label:] for (variable in object){BLOCK}
See break and continue for more information about the use of label.
Example:
var obj = "someObject";
var result = " ";
for ( var i in obj )
{
  result += obj + "." + i + " = " + obj[i] + "\n"
}
  document.write( result );
function
Declares a function with the specified parameters and method. Acceptable parameters include strings, numbers, and objects. The method can be any combination of JavaScript expressions. This is preferred method to create a function as it is the most efficient. A function will not return a result unless a return statement is executed. Error handling can be implemented with the try, throw, catch, and finally statements.
Syntax: function name( param1, param2, param3 ){BLOCK}
See also: return
Example of function with no return value:
function myFunction(x)
{
  document.write("x = " x)
}
if
The basic conditional statement that executes a set of statements if a specified condition is true.
Syntax: if (CONDITION) {BLOCK}
See also: if...else
Example:
if (myBool == true)
{
  document.write("myBool is true");
  myBool = false
}
if...else
A conditional statement that executes one set of statements if a specified condition is true and another if the condition is false.
Syntax: if (CONDITION) {BLOCK} else {BLOCK}
See also: if
Example:
if (myBool == true)
{
  document.write("myBool is true");
  myBool = false
}
else
{
  document.write("myBool is false");
  myBool = true
}
return
Specifies the value to be returned by a function.
Syntax: return expression
See also: function statement, Function class
Example of function with a return value:
function mySquare(x)
{
  return x * x
}
switch
The basic program control statement that allows a program to evaluate a target expression and attempt to match the expression's value to a case label. The target expression can be a variable, property, or even a numerical, comparison, or string expression (e.g. myVar, screen.width, x +4, x < 6, or "foo" + someString).
Syntax: switch target_expression {BLOCK}
The block consists of one or more case, case...break, or default statements.
Example:
switch theFruit
{
  case apple: {document.write("apples")};
  case banana: {document.write("bananas"); break};
  default: {document.write("neither apples nor bananas")}
}

throw
Halts execution immediately and throws a user defined exception. The exception thrown can be a Boolean, numeric, or string value for simple exceptions, or an object for complex exceptions. Throwing an exception is essentially a call to the catch(e) error handling function. The try statements tests for the thrown exception and the catch statement handles the exception.
Syntax: throw e
See try and catch.
Example:
function mySquare(x)
{
  if ( x == NaN){throw "mySquare: x is not a number");
  return x * x
}
try
Contains the portion of executable code for a try statement that is monitored for thrown exceptions.
Syntax: try {BLOCK }
See also catch, finally, and throw.
Example of a function with errors handled:
function mySquare(x)
{
  try
  {
    if ( x == NaN){throw "mySquare: x is not a number")
  }
  catch(e)
  {
    if ( e == "mySquare: x is not a number")
    {
      window.alert("mySquare: x is not a number');

            x = 0
        }
    }
    finally
   {
      return x * x
    }
}
var
Declares a private variable and can optionally initialize it to a value. Private variables exist only within the scope of the current statement or function (inside the current set of curly braces)
Syntax: var varname [= value]
Variables declared without var are public variables and exist both within and without the scope. To prevent possible conflicts, all variables should be declared private except for those that need to be publicly exposed. Whether public or private, the type of value it is initialized with sets its type (Boolean, number, string, function, array, etc).
Example:
var myNumber = 1.27;
var myString = "Some String";
var myBoolean = true;
while
A loop constructor that creates a loop that evaluates an expression and, if it is true, executes a block of statements. The loop then repeats as long as the specified condition remains true. Note that the statement block may never execute. The while loop is used when the conditions for loop termination are not known prior to execution of the loop and are to be determined outside the loop execution. The optional break statement will cause loop execution to halt and passes control to the next statement following the loop.  The optional continue statement will halt loop execution at that point and passes control to the condition expression. The optional label allows the specification of which loop is to broken or continued in the case of nested loops.
Syntax: [label:] while (CONDITION){BLOCK}
See also: do...while and for
See break and continue for more information about the use of label.
Example:
var n = 0;
while ( done != false )
{
  document.write(n);
  n++
}
with
Establishes the default object for a scope.
Syntax: with(objectName){BLOCK}
Example:
var a, b, c;
with (Math)
{
  a = sin(c};
  b = cos(c)
}