| break | continue | else | false | for | function |
| if | in | new | null | return | this |
| true | var | void | while | with | switch & case |
Escaping from an infinite loop with a break.
while (true) {
n++;
if (values[n] == 1) break;
}
switch & case
Index
continue can be used to skip part of a loop and go to the next loop iteration.
for (i = 1; i < 21; i++) {
if (score[i] == 0) continue;
document.write ("Student number ", i, "Score: ", score[i], "\n");
}
for
while
Index
if (a == 1) {
alert ("Found a 1!");
a = 0;
}
else {
alert ("Incorrect value : " + a);
}
if
Index
Boolean false
IndexA for loop below will execute from the first to the twenty-first iteration, stepping in increments of 1. The second loop will step backwards from 10 to 0 in decrements of 2.
for (i = 1; i < 21; i++) {
if (score[i] == 0) continue;
document.write ("Student number ", i, "Score: ", score[i], "\n");
}
for (i = 10; i == 0; i--) {
if (score[i] == 0) continue;
document.write ("Student number ", i, "Score: ", score[i], "\n");
}
while
continue
in
Index
Functions are groups of JavaScript statements which can be treated as a single unit. Functions can also be accessed from any window within another. For instance, if a function is declared in a parent frame then the function can be called using the parent prefix. Below is an example of a function.
function Sum (str, elements, delimiter)
{
if (str.length > 0 && elements > 0 && delimiter != null && delimiter != "")
{
var numbers = new Array(elements);
var i;
var j;
numbers = str.split (delimiter);
j = 0;
for (i = 0; i < elements; i++)
{
if (numbers[i])
{
j += parseInt (numbers[i]);
}
}
return (j);
}
return (0);
}
return
Index
if statements are conditional expressions.
if (a == 1) value = 1; else value = 0;
Combining conditions with logical operators.
if (phone == " ") window.alert ("error!");
if (email == " ") window.alert ("error!");
These two statements can be combined into a single statement using a logical operator.
if (phone == " " || email == " ") window.alert ("error!");
&& is the and logical operator and || is the or logical operator.
In addition JavaScript provides of the conditional expression that can be used to make quick decisions. This allows embedding of the conditional expression. The shorthand conditional statement has the following syntax.
variable = (condition) : if true : if false;
This is the equivalent of the if statement above.
value = (a == 1) ? 1 : 0;
Here is an example of a functionally embedded if statement.
document.write ("Found " + counter + ((counter == 1) ? "word." : "words."));
else
Index
if (a == 1) value = 1; else value = 0;if Index
The for ... in loop is designed for doing an operation on each property of an object.
for (i in navigator) {
document.write ("property: " + i);
document.write (" value: " + navigator[i]);
}
for
Index
The new keyword allows the creation of a new objects.
john = new Card ();
john.name = "John Smith";
john.number = "1234 567890 1234";
john.expiry = "09/99";
jim = new Card("Jim Smythe", "1234 567890 5678", "01/99");
this
with
Index
The keyword null represents the value of an undefined variable, ie. when a variable has not been previously used, defined or is out of scope.
IndexThe return keyword is used to pass a value back from a fumction.
function Average (str, elements, delimiter)
{
if (str.length > 0 && elements > 0 && delimiter != null && delimiter != "")
{
var numbers = new Array(elements);
var i;
var j;
numbers = str.split (delimiter);
j = 0;
for (i = 0; i < elements; i++)
{
if (numbers[i])
{
j += parseInt (numbers[i]);
}
}
return (j / elements);
}
return (0);
}
function
Index
The this keyword references the currently referenced object.
function CreditCard (name, number, expiry)
{
this.name = name;
this.number = number;
this.expiry = expiry;
}
Boolean true
IndexThe var keyword is optional and is used to declare variables.
There are two types of variables.
The scope of a variable is the area or region in which a variable is usable. Thus a local variable is only usable within the function within which it is declared. Note that JavaScript does not require explict declaration of variables (Visual Basic and Perl will allow this. Although Perl does weird and unexpected things with what are called local variables). Therefore care must be taken when using non-explicitly declared variables in JavaScript. For instance, once could declare a local variable in a function. One could also use the same variable name outside of the function. It is important to note that the variable used outside the function will not have the same value as that of the variable in the function. I have not as yet tried this but that is the general theory anyway. Non explicity declared variables are generally a bit of a nightmare for highly disciplined programmers.
The script shown below declares name1 and name2 globally. str and Mesg are declared locally to the Msg function. name1 and name2 are set to Joe and Harry respectively. Setting of name2 inside the function resets name2 to Larry since name2 is a global variable. Changing Mesg outside the function has no effect. In fact there will be a second declaration of Mesg outside the function. There are actually two declarations of Mesg within the HTML page. One is global to the entire page. The other is active only in the function. In fact the global declaration of Mesg is ignored inside the function. Once again, that is the theory. How theory applies in JavaScript I am not as yet sure of.
var name1 = "Joe";
var name2 = "Harry";
function Msg (str)
{
var Mesg = "Hi ";
document.writeln (Mesg + str);
name2 = "Larry";
}
...
Msg (name1);
Msg (name2);
Msg (name2);
Mesg = "Goodmorning ";
Msg (name1);
Index
The void keyword is usually used to indicate nothing returned from a function or a method. There does not appear to be any reference to the void in any of my JavaScript books.
IndexThe while loop will loop as long as the condition is met. There are two types of while loop. The first loop shown below checks the condition at the start of the loop. The second loop shown below checks the condition at the end of the loop. This second type of
while loop is also theoretically known as an until such that the loop is executed until a condition is met. Thus the first loop will not necessarily execute and the second loop will execute atleast once.
while (total < 10) {
n++;
total += values[n];
}
do {
n++;
total += values[n];
} while (total < 10);
for
continue
Index
The with keyword specifies an object. It is followed by a block of statements which apply specifically to the object to which the with keyword points.
var name = new String ("This is a string");
with (name)
{
document.write (length);
}
The statements above give the length of the string This is a string which is 16.
new this IndexA break statement must always be used with a switch-case statement otherwise all following options will also be executed.
switch (button) {
case "next" :
window.location = "next.html";
break;
case "previous" :
window.location = "previous.html";
break;
case "home" :
window.location = "home.html";
break;
case "back" :
window.location = "menu.html";
break;
default :
window.alert ("Wrong button");;
}
break
Index