JavaScript Statements
Statements are the smallest standalone elements of the JavaScript language.
Statements:
Method | Support | Description | |||||
---|---|---|---|---|---|---|---|
break | The break statement can be used to terminate a loop (while, for, for...in, or do...while), or break out of a switch statement. | ||||||
const | Declares a read-only variable. | ||||||
continue | Stops the current iteration and continues the loop with the next step. | ||||||
debugger | Opens the browser specific script debugger application. | ||||||
do...while | Executes a specified code repeatedly until the condition expression in the while clause (while(condition)) evaluates to false. | ||||||
export | Provides access to the properties, functions and objects of a signed script for another signed or unsigned script. | ||||||
for | Creates a loop that is executed repeatedly while the specified condition is true. | ||||||
for...in | Iterates a specified variable over all properties of an Object, or all elements in an Array, in arbitrary order, and executes the statement for each property or element. | ||||||
for each...in | Iterates a specified variable over all properties of an Object. Same as for...in, but it can only be used for objects. | ||||||
function | Declares a new function with the given parameters. | ||||||
if...else | Executes different code depending on whether the condition in the if clause is true or false. | ||||||
import | Enables a script to use the properties, functions and objects of another script that have been exported before. | ||||||
labeled statement | Provides an identifier for a statement that can be referred to in a break or continue statement. | ||||||
return | Stops the execution of a function and exits with the specified return value. | ||||||
switch...case | Allows the execution of different code in different cases. The switch statement provides a way to create a complex case-selection without the use of the if...else statement. | ||||||
this | Returns a reference to the current object. | ||||||
throw | Throws an exception that can be handled within the catch block of a try...catch statement to identify the type of script error. | ||||||
try...catch | Allows to handle of script generated errors. | ||||||
var | Declares a variable. | ||||||
while | Executes the specified code repeatedly while the condition expression in the while clause (while(condition)) evaluates to true. | ||||||
with | Specifies the object for a set of statements. |
break
The break statement can be used to terminate a loop (while, for, for...in, or do...while), or break out of a switch statement.
The break statement exits from the current loop or switch statement, and causes program control to continue execution immediately after statement.
Syntax:
break [label];
Example 1
The output:
Alerts 'zero', 'one or two', 'one or two', 'some other value'. Example 2
The output:
Alerts 'AB'. The str variable contains only the first two elements of the arr array, because the break statement exits the for loop when it reaches the array element "C". Example 3
The output:
Writes '(0, 0), (0, 1), (0, 2), (1, 1)', because the break MainLoop statement jumps over the first for loop, when i is one and j is two. |
|||||||||||||||||||||||||||||||||||||||||||||
const
Declares a read-only variable.
A read-only variable cannot be redeclared and its value cannot be modified. Although this statement is supported by Opera, the read-only feature is not implemented, so it works the same as the var statement.
Syntax:
const variable1 [=value1], variable2 [=value2], ..., variableN [=valueN];
Example 1
|
|||||||||||||||||||||||||||||||||||||||||||||
continue
Stops the current iteration and continues the loop with the next step.
The continue statement is similar to the break statement, in the sense that it also terminates the execution of the loop, but not entirely.
Syntax:
continue [label];
Example 1
The output:
Writes 'ABD', because the continue statement causes the program to continue with the next iteration of the for statement. Example 2
The output:
Writes 'ABD', because the continue statement causes the program to continue at the beginning of the while statement. Example 3
The output:
Writes '(0, 0), (0, 1), (0, 2), (1, 1), (2,2)', because the continue MainLoop statement jumps to the beginning of the first for loop, when i is one and j is two. |
|||||||||||||||||||||||||||||||||||||||||||||
debugger
Opens the browser specific script debugger application.
Syntax:
debugger;
Example 1
You can watch the str variable in the debugger.
|
|||||||||||||||||||||||||||||||||||||||||||||
do...while
Executes the statements within the loop first. While the given condition (while(condition)) evaluates to true, the statements within the loop are executed repeatedly.
Syntax:
do {
[statements] } while (condition);
Example 1
The output:
The str variable contains 'AB', because the while statement returns true only until the idx variable is lower than 2. |
|||||||||||||||||||||||||||||||||||||||||||||
export
Provides access to the properties, functions and objects of a signed script for another signed or unsigned script.
The property, object or function must be exported first by the signed script, and then it must imported by the import statement in the script where the member is to be used. The Firefox export/import feature only works for signed scripts. To get more information about signed scripts, please visit the Signed Scripts in Mozilla page.
Syntax:
export name1, name2, ..., nameN;
export *;
|
|||||||||||||||||||||||||||||||||||||||||||||
for
Creates a loop that is executed repeatedly while the specified condition is true.
Syntax:
for ([init]; condition; [next]) {
[statements] }
Example 1
The i variable is declared with an initial value of 0.
After that, the for statement checks whether the value of i is lower than 10. If it is lower, it executes the document.write method. At the end of the loop, the value of i is incremented by one (i++) and the first iteration is finished. Before the next iteration, it checks the value of i. If it is lower than 10, the loop is continued, else it is terminated. Example 2
This example iterates over the indeces of an array, in ascendent order.
Example 3
This example iterates over the indeces of an array, in descendant order.
|
|||||||||||||||||||||||||||||||||||||||||||||
for...in
Iterates a variable over the names of an object's members and executes the specified statements in each loop.
Syntax:
for (variable in object) {
[statements] }
Example 1
The i variable iterates over the names of the obj's members and the write method
prints the name and the value of the members into the document.
Example 2
The i variable iterates over the names of the window.navigator object's members, and the write method
prints the name of members into the document.
Example 3
Do not use the for...in statement to iterate over the indices of an array!
Indices of an array behave like member names, so the for...in statement iterates over them, but if the array has other members, the for...in statement also finds of them. If you want to iterate over the indices of an array, use the for statement and the length property of the array. |
|||||||||||||||||||||||||||||||||||||||||||||
for each...in
Iterates a variable over the values of an Object's members and executes the specified statements in each loop.
The for...in statement is similar to the for each...in statement, but it iterates over the names not the values of members.
Syntax:
for each (variable in object) {
[statements] }
Example 1
The i variable iterates over the values of the obj's members and the write method
prints the values into the document.
Example 2
Do not use the for each...in statement to iterate over the elements of an array!
Indices of an array behave like member names, so the for each...in statement iterates over their values, but if the array has other members, the for each...in statement also finds of them. If you want to iterate over an array, use the for statement and the length property of the array. |
|||||||||||||||||||||||||||||||||||||||||||||
function
Declares a new function with parameters.
When calling a function with fewer parameters than declared, the rest of the parameters will be undefined inside the function. If you need a variable-length parameter list, use the arguments object inside the function to get all parameters by the specified caller. See Example 2 and the page for the arguments object for details. Use the return statement within a function to return a value.
Syntax:
function name ([param1] [, param2] [..., paramN]) {
[statements] }
Example 1
The GetSum function returns the sum of the given two numbers.
Example 2
The GetSum function returns the sum of the given numbers.
|
|||||||||||||||||||||||||||||||||||||||||||||
if...else
Executes different statements depending on the value of a condition.
Syntax:
if (condition) {
[statements1] } [else { [statements2] }]
Example 1
If the 'x' variable is equal to 1, y set to 2.
Example 2
If the 'x' variable is equal to 1 and the 'y' variable is equal to 2, then 'z' is set to 3, but if the
'x' variable is 1 and 'y' is not equal to 2, then 'z' is set to 4.
|
|||||||||||||||||||||||||||||||||||||||||||||
import
Enables a script to use properties, functions and objects of a signed script that have been exported before.
The property, object or function must be exported first by the signed script, and then it must imported by the import statement in the script where the member is to be used. The Firefox export/import feature only works for signed scripts. To get more information about signed scripts, please visit the Signed Scripts in Mozilla page.
Syntax:
import object.name1, object.name2, ..., object.nameN;
import object.*;
|
|||||||||||||||||||||||||||||||||||||||||||||
label
Provides an identifier for a statement that can be used to be referred to by a break or continue statement.
Labels are commonly used to identify the point where the execution of the code will be continued.
Syntax:
label:
statement
Example 1
The output:
Writes '(0, 0), (0, 1), (0, 2), (1, 1)', because the break MainLoop statement jumps over the first for loop, when i is one and j is two. |
|||||||||||||||||||||||||||||||||||||||||||||
return
Stops the execution, and exits from a function with the specified return value.
Syntax:
return [value];
Example 1
The GetSum function returns the sum of the given two numbers (in this example: 24).
|
|||||||||||||||||||||||||||||||||||||||||||||
switch...case
Allows the execution of different code in different cases. The switch statement provides a way to create a complex case-selection.
Program control searches for a match between the labeled cases for the value and the type of the expression specified in the switch statement. If a match is found, execution continues after the first matching case clause. If no match is found, execution continues after the default clause or after the end of the switch statement if a default clause is not defined.
Syntax:
switch (expression) {
case label1: [statements] [break;] case label2: [statements] [break;] default: [statements] [break;] }
Example 1
The output:
Alerts 'zero', 'one or two', 'one or two', 'some other value'. Example 2
The output:
Alerts 'zero', 'one or two'. |
|||||||||||||||||||||||||||||||||||||||||||||
this
Returns a reference to the current object.
Typically used in methods of objects or in event handlers to refer to the current object.
Note that the this keyword refers to the window object in the following cases:
Syntax:
this;
Example 1
This example shows how to pass the object to an event handler on which the event listener was registered.
Note that the object on which the event has occurred and the object referred to by this may be different.
See the next example.
Example 2
This example checks whether the element on which the event has occurred and the element on which the event was regitered are the same.
Example 3
This example illustrates the use of the this statement for objects.
Example 4
This example shows how to register object methods as event handlers.
Example 5
This example tests the this keyword in some cases.
|
|||||||||||||||||||||||||||||||||||||||||||||
throw
Allows throwing an exception that can be handled in the catch block of a try...catch statement to identify the type of an error.
Syntax:
throw expression;
Example 1
This example throws an exception if the given string is not a number or is not between 0 and 5.
|
|||||||||||||||||||||||||||||||||||||||||||||
try...catch
Allows to handle script generated errors for the statements inside the try block.
When an error occurs inside the try block, it raises an exception. The exception can be catched with the catch statement.
An exception can be thrown directly with the throw statement, too.
With the finally statement, you can define statements that are always executed, regardless of whether an exception has occurred or not.
Syntax:
try {
try_statements } [catch (exception){ [catch_statements] }] [finally { [finally_statements] }]
Example 1
This example catches an exception and displays information about the error.
Example 2
This example throws an exception if the given string is not a number or is not between 0 and 5.
|
|||||||||||||||||||||||||||||||||||||||||||||
var
Declares a variable with local scope.
A variable can be defined with or without the var keyword, but syntactically the var keyword is needed. If the var keyword is not used, the variable is undeclared. When a variable is defined with the var keyword, it can only be used within the block where it is defined. When a variable is defined without the var keyword, it is created as a global variable and can be reached from anywhere in the document, but it is recommended to declare global variables with the var keyword placed outside any function. See the examples below for details.
Syntax:
var variable1 [=value1], variable2 [=value2], ..., variableN [=valueN];
Example 1
This example declares variable x with local scope and variable y with global scope.
|
|||||||||||||||||||||||||||||||||||||||||||||
while
Executes the code within the loop repeatedly while the given condition (while(condition)) evaluates to true.
Syntax:
while (condition) {
[statements] }
Example 1
|
|||||||||||||||||||||||||||||||||||||||||||||
with
Marks an object for a set of statements. When the JavaScript interpreter analyzes the code inside a with block and finds an identifier, it tries to find it among the object's members first.
Do not use too many with statements in your code, because it causes slower processing.
Syntax:
with (object) {
[statements] }
Example 1
In this example the with statement specifies that the Math object is the default object. The pow and the sqrt
methods are called without any object, but the interpreter assumes the Math object for these methods.
Example 2
In this example the with statement specifies that the div.style object is the default object. The backgroundColor
property is a member of the div.style object.
Example 3
If you use the with statement, be careful with the specified object members. In this example the backgroundColor will be "red" not "blue"!
|
User Contributed Comments