JavaScript Editor JavaScript Validator     JavaScript Editor 



Team LiB
Previous Section Next Section

JavaScript Statements

The following tables describe core JavaScript statements.

Block

Statement

Introduced

Description

{ }

JavaScript 1.5

Used to group statements as delimited by the curly brackets.

Conditional

Statement

Introduced

Description

else if

JavaScript 1.2

Executes a set of statements if a specified condition is true. Another set of statements can be executed if the statement is false.

switch

JavaScript 1.2

Executes a branch in the flow of a program's execution.

Declarations

Statement

Introduced

Description

Var

JavaScript 1.0 Jscript 1.0

Used to declare a variable. Initializing it to a value is optional at the time of declaration.

function

JavaScript 1.0 Jscript 1.0

Used to declare a function with the specified parameters, which can be strings, numbers, or objects. To return a value the function must use the return statement.

Loop

Statement

Introduced

Description

Do...while

JavaScript 1.2 Jscript 3.0

Not in ECMA-262. Executes the statements specified until the test condition after the while evaluates to false. The statements are executed at least once because the test condition is evaluated last.

for

JavaScript 1.0 Jscript 1.0

Creates a loop controlled according to the three optional expressions enclosed in the parentheses after the for and separated by semicolons. The first of these three expressions is the initial-expression, the second is the test condition, and the third is the increment-expression.

for...in

JavaScript 1.0 Jscript 1.0

Used to iterate over all the properties of an object using a variable. For each property the specified statements within the loop are executed.

If...else

JavaScript 1.0 Jscript 1.0

Executes a block of statements if the condition evaluates to true. If the condition evaluates to false, another block of statements can be executed.

while

JavaScript 1.0 Jscript 1.0

Executes a block of statements if a test condition evaluates to true. The loop then repeats, testing the condition with each repeat, ceasing if the condition evaluates to false.

break

JavaScript 1.0 Jscript 1.0

Used within a while or for loop to terminate the loop and transfer program control to the statement following the loop. Can also be used with a label to break to a particular program position outside of the loop.

label

JavaScript 1.2 Jscript 3.0

An identifier that can be used with break or continue statements to indicate where the program should continue execution after the loop execution is stopped.

Execution Control Statements

Statement

Introduced

Description

continue

JavaScript 1.0 Jscript 1.0

Used to stop execution of the block of statements in the current iteration of a while or for loop; execution of the loop continues with the next iteration.

return

JavaScript 1.0 Jscript 1.0

Used to specify the value to be returned by a function.

switch

JavaScript 1.2 Jscript 3.0

Not in ECMA-262. Specifies various blocks of statements to be executed depending on the value of the expression passed in as the argument. Similar to a Visual Basic Select Case statement.

with

JavaScript 1.0 Jscript 1.0

Specifies the default object for a set of statements.

Exception Handling Statements

These were introduced in JavaScript 1.4.

Statement

Introduced

Description

throw

JavaScript 1.4 Jscript 5.0

Not in ECMA-262. Throws a custom exception defined by the user.

try...catch

  

...finally

JavaScript 1.4 Jscript 5.0

Not in ECMA-262. Executes the statements in the try block; if any exceptions occur, these are handled in the catch block. The finally block allows us to stipulate statements that will be executed after both the try and catch statements.

Object Manipulation Statements

Statement

Introduced

Description

for...in

JavaScript 1.0

Iterates a specific variable over object properties.

with

JavaScript 1.0

 

Conditional Compile Statements

These are specific to Jscript and are not supported by Netscape Navigator. Introduced in Jscript 3.0. These statements allow Jscript code to be compiled only if certain conditions are met. Conditional compilation also provides access to the conditional compilation variables, which provide information about the platform and version of Script in use. Conditional compilation statements should be placed inside comments, where they will be ignored by browsers that do not support conditional compilation.

Statement

Introduced

Description

@cc_on

Jscript 3.0

Turns conditional compilation on.

@if...@elif...@else...@end

Jscript 3.0

Compiles the following code only if the given condition equates to true. The @elif statement allows for another condition to be tested, and @else supplies a default if no conditions are met.

@set

Jscript 3.0

Allows variables to be created for use in conditionally compiled code. The variable must start with the character @.

Predefined Conditional Compilation Variables

These predefined variables return information about the system and platform on which the browser is running and can be accessed when conditional compilation is enabled.

Variable

Description

@_alpha

Returns true if the browser is running on a DEC Alpha machine, otherwise NaN.

@_Jscript

Returns true if the browser supports Jscript. This always returns true.

@_Jscript_build

Returns the build number for the version of Jscript in use.

@_Jscript_version

Returns the version number for the version of Jscript in use.

@_mac

Returns true if the browser is running on an Apple Macintosh machine, otherwise NaN.

@_mc680x0

Returns true if the browser is running on a Motorola 680x0 machine, otherwise NaN.

@_PowerPC

Returns true if the browser is running on a Motorola Power PC machine, otherwise NaN.

@_win16

Returns true if the browser is running on a Windows 16-bit platform, otherwise NaN.

@_win32

Returns true if the browser is running on a Windows 32-bit platform, otherwise NaN.

@_x86

Returns true if the browser is running on an IBM PC or compatible, otherwise NaN.

Other Statements

Statement

Introduced

Description

comment

JavaScript 1.0 Jscript 1.0

Notes, which are ignored by the script engine, and which can be used to explain the code.

import

JavaScript 1.2

Allows a script to import objects, and their properties and methods, which have been exported from another script. Not supported by Internet Explorer.

export

JavaScript 1.2

Allows a signed script to export objects, and their properties and methods, so that they can be imported by other scripts. Not supported by Internet Explorer.


Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Validator     JavaScript Editor


©