JavaScript Tutorial
3. Statements
[this page | pdf | back links]
JavaScript
statements identify instructions that are executed by the web browser. For
example, the following statement tells the browser to write “Hello World”
inside an HTML statement with the id attribute = "element":
document.getElementById("element").innerHTML
= "Hello World"
The same result can be
achieved using several separate statements, e.g.:
var d = document.getElementById("element");
var x = "Hello";
var y = " World";
var z = x + y;
d.innerHTML = z;
Statements are separated
by semicolons and multiple statements are allowed on one line. JavaScript
ignores multiple spaces (except in strings, i.e. within quotation marks). A
common good practice is to put spaces around operators (e.g. =, +
, …). Very long lines of code are also often frowned upon, and are usually
broken after an operator.
Statements can (and often
are) grouped together in code blocks, inside curly brackets, i.e. { … }. A
particularly important example of the use of code blocks involves functions,
which provide a means of executing on demand one or more statements, e.g.:
function func() {
document.getElementById("element").innerHTML
= "Hello";
}
Statements often start
with a statement identifier. These are reserved words which cannot be used as
variable names or for other purposes. A list of statement reserved words
recognised by JavaScript is shown here. They
include: break, continue, do, for, if, return,
switch, throw, try,
catch, var and while.
Most
JavaScript programs contain many statements, which are executed one by one in
the order in which they are written except when statement flow control is
adjusted using statements such as for,
if or while.
NAVIGATION LINKS
Contents | Prev | Next | JavaScript