JavaScript Control Statements
This page descibes the use of JaveScript control statements such as the if statement, while statement, the for statement, and the switch statement. It provides examples.
if
if (value == 3)
{
document.write("Value is three")
}
else
{
document.write("Value is not three")
}
While
var i = 0
while (i < 3)
{
i++
}
Do While
The example prints the days of the week.
days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var day = 0
do
{
document.write(days[day] + " is day " + eval(day+1) + " of the week.<BR>\n")
day++
} while (day < 7)
|
|
For
The following example will determine what numeric day of the week Wednesday falls on.
days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var day = "Wednesday"
var place
for (i = 0;i < days.length;i++)
{
if (day == days[i])
{
place = i + 1
}
}
document.write(day + " is day " + place + " in the week.<BR>\n")
The switch statement
Runs one or more commands based on the value of the expression being evaluated. An example is:
switch (day) {
case 1:
document.write("Sunday<BR>\n")
break
case 2:
document.write("Monday<BR>\n")
break
case 3:
document.write("Tuesday<BR>\n")
break
case 4:
document.write("Wednesday<BR>\n")
break
case 5:
document.write("Thursday<BR>\n")
break
case 6:
document.write("Friday<BR>\n")
break
case 7:
document.write("Saturday<BR>\n")
break
default:
document.write("Invalid Weekday<BR>\n")
break
}
Break
The break statement may be used to break out of a while, if, switch, dowhile, or for statement. The break causes program control to fall to the statement following the iterative statement. The break statement now supports a label which allows the specification of a location for the program control to jump to. The following code will print "Wednesday is day 4 in the week".
days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var day = "Wednesday"
var place
for (i = 0;i < days.length;i++)
{
if (day == days[i]) break;
}
place = i + 1
document.write(day + " is day " + place + " in the week.<BR>\n")
Continue
The continue statement does not terminate the loop statement, but allows statements below the continue statement to be skipped, while the control remains with the looping statement. The loop is not exited. The example below does not include any ratings values below 5.
ratings = new Array(6,9,8,4,5,7,8,10)
var sum = 0;
var reviews = 0
for (i=0; i < ratings.length; i++)
{
if (ratings[i] < 5) continue
sum += ratings[i];
reviews++;
}
var average = sum/reviews
For in Statement
Executes a loop similar to a for statement that executes for all the properties in an object. The example below lists all properties and their values for the document object. The document is the HTML page being displayed.
for (i in document)
{
document.write("Property = " + i + " Value = " + document[i] + "<BR>\n")
}
A few of the properties and their values are:
Property = activeElement Value = [object]
Property = alinkColor Value = #ff0000
Property = all Value = [object]
Property = anchors Value = [object]
Property = applets Value = [object]
Property = bgColor Value = #ffffff
Property = body Value = [object]
Property = cookie Value = pageCount=9
Property = defaultCharset Value = iso-8859-1
Property = domain Value =
Property = embeds Value = [object]
Property = fgColor Value = #000000
Property = fileCreatedDate Value = Thursday, September 21, 2000
.
.
With Statement
Eliminates references to the object within the statement by identifying the default object to be used inside the braces. This example prints out the document properties and values without referencing the document object to perform the function.
with (document)
{
for (i in this)
{
write("Property = " + i + " Value = " + document[i] + "<BR>\n")
}
}
|