April 2002 Draft
JavaScript 2.0
Core Language
Statements
previousupnext

Wednesday, February 20, 2002

Statements

Most of the behavior of statements is the same as in JavaScript 1.5. Differences are highlighted below.

  {abbrevnoShortIffull}
Statement 
   ExpressionStatement Semicolon
|  SuperStatement Semicolon
|  Block
|  LabeledStatement
|  IfStatement
|  SwitchStatement
|  DoStatement Semicolon
|  WhileStatement
|  ForStatement
|  WithStatement
|  ContinueStatement Semicolon
|  BreakStatement Semicolon
|  ReturnStatement Semicolon
|  ThrowStatement Semicolon
|  TryStatement
Substatement 
   EmptyStatement
|  Statement
|  SimpleVariableDefinition Semicolon
|  Attributes [no line break] { Substatements }
Substatements 
   «empty»
|  SubstatementsPrefix Substatementabbrev
SubstatementsPrefix 
   «empty»
|  SubstatementsPrefix Substatementfull
Semicolonabbrev 
   ;
|  VirtualSemicolon
|  «empty»
SemicolonnoShortIf 
   ;
|  VirtualSemicolon
|  «empty»
Semicolonfull 
   ;
|  VirtualSemicolon

A Substatement is a statement directly contained by one of the compound statements label:, if, switch, while, do while, for, or with (but not a block). A substatement cannot be a directive except that, in non-strict mode only, it can be a var definition without attributes or types.

A substatement can also consist of one or more attributes applied to a group of substatements enclosed in braces. The attributes must evaluate to either true or false. The braces do not form a scope in this case.

The Semicolon productions allow both grammatical and line-break semicolon insertion.

Empty Statement

EmptyStatement  ;

Expression Statement

ExpressionStatement  [lookahead{function{}] ListExpressionallowIn

Under some circumstances an ExpressionStatement is treated as a constructor call instead of an expression to be evaluated. See calling a superconstructor.

Super Statement

SuperStatement  super Arguments

The super statement calls the superclass’s default constructor. It can only be used inside a class’s constructor. There are also other ways of calling a specific constructor in the superclass or in the current class; see calling a superconstructor.

Block Statement

Block  { Directives }

A block groups statements and forms a scope.

Labeled Statements

LabeledStatement  Identifier : Substatement

If Statement

IfStatementabbrev 
   if ParenListExpression Substatementabbrev
|  if ParenListExpression SubstatementnoShortIf else Substatementabbrev
IfStatementfull 
   if ParenListExpression Substatementfull
|  if ParenListExpression SubstatementnoShortIf else Substatementfull
IfStatementnoShortIf  if ParenListExpression SubstatementnoShortIf else SubstatementnoShortIf

The semicolon is optional before the else.

Switch Statement

SwitchStatement  switch ParenListExpression { CaseStatements }
CaseStatements 
   «empty»
|  CaseLabel
|  CaseLabel CaseStatementsPrefix CaseStatementabbrev
CaseStatementsPrefix 
   «empty»
|  CaseStatementsPrefix CaseStatementfull
CaseStatement 
   Substatement
|  CaseLabel
CaseLabel 
   case ListExpressionallowIn :
|  default :

Do-While Statement

DoStatement  do Substatementabbrev while ParenListExpression

The semicolon is optional before the closing while.

While Statement

WhileStatement  while ParenListExpression Substatement

For Statements

ForStatement 
   for ( ForInitialiser ; OptionalExpression ; OptionalExpression ) Substatement
|  for ( ForInBinding in ListExpressionallowIn ) Substatement
ForInitialiser 
   «empty»
|  ListExpressionnoIn
|  VariableDefinitionKind VariableBindingListnoIn
|  Attributes [no line break] VariableDefinitionKind VariableBindingListnoIn
ForInBinding 
   PostfixExpression
|  VariableDefinitionKind VariableBindingnoIn
|  Attributes [no line break] VariableDefinitionKind VariableBindingnoIn

A for statement forms a scope. Any definitions in it (including the ForInitialiser and ForInBinding) are visible inside the for statement and its substatement, but not outside the for statement. However, a var definition inside a for statement may be hoisted to the nearest enclosing regional scope.

With Statement

WithStatement  with ParenListExpression Substatement

Continue and Break Statements

ContinueStatement 
   continue
|  continue [no line break] Identifier
BreakStatement 
   break
|  break [no line break] Identifier

Return Statement

ReturnStatement 
   return
|  return [no line break] ListExpressionallowIn

Throw Statement

ThrowStatement  throw [no line break] ListExpressionallowIn

Try Statement

TryStatement 
   try Block CatchClauses
|  try Block FinallyClause
|  try Block CatchClauses FinallyClause
CatchClauses 
   CatchClause
|  CatchClauses CatchClause
CatchClause  catch ( Parameter ) Block
FinallyClause  finally Block

Each CatchClause forms a scope. The Parameter, if any, is defined as a local variable visible only within the CatchClause.

The Blocks following try and finally are also scopes like other Block statements.

Directives

Directive 
   EmptyStatement
|  Statement
|  AnnotatableDirective
|  Attributes [no line break] AnnotatableDirective
|  Attributes [no line break] { Directives }
|  PackageDefinition
|  IncludeDirective Semicolon
|  Pragma Semicolon
AnnotatableDirective 
   ExportDefinition Semicolon
|  VariableDefinition Semicolon
|  FunctionDefinition
|  ClassDefinition
|  NamespaceDefinition Semicolon
|  InterfaceDefinition
|  ImportDirective Semicolon
|  UseDirective Semicolon
Directives 
   «empty»
|  DirectivesPrefix Directiveabbrev
DirectivesPrefix 
   «empty»
|  DirectivesPrefix Directivefull

Attributes can be applied to a group of directives by following them by a {, the directives, and a }. The attributes apply to all of the enclosed directives. The attribute true is ignored. The attribute false causes all of the enclosed directives to be omitted. When used this way, the braces do not form a block or a scope.

Annotated groups are useful to define several items without having to repeat attributes for each one. For example,

class foo {
  var z:Integer;
  public var a;
  private var a;
  private function f() {}
  private function g(x:Integer):Boolean {}
}

is equivalent to:

class foo {
  var z:Integer;
  public var a;
  private {
    var b;
    function f() {}
    function g(x:Integer):Boolean {}
  }
}

Include Directive

IncludeDirective  include [no line break] String

An include directive includes a JavaScript source file at the current position. The interpretation of String is implementation-defined but recommended to be similar to the interpretation of a string in an import directive.

The included file should contain a syntactically balanced JavaScript program that is processed before processing the rest of the program that contains the include directive.

Programs

Program  Directives

Waldemar Horwat
Last modified Wednesday, February 20, 2002
previousupnext