HTML and CSS Reference
In-Depth Information
missing or
null
value, that value would be added to the
total
variable, resulting in a
null
total. One way to fix this problem would be to use the
continue
statement, jump-
ing out of the current iteration if a missing or
null
value were encountered. The revised
code would look like the following:
var total = 0;
for (var i = 0; i < data.length; i++) {
if (data[i] == null) continue; // continue with the next
iteration
total += data[i];
}
Exploring Statement Labels
statement labels
are used to identify statements in JavaScript code so that you can refer-
ence those lines elsewhere in a program. The syntax of the statement label is
label
:
statements
where
label
is the text of the label and
statements
is the statements identified by the
label. You've already seen labels with the
switch
statement, but they also can be used
with other program loops and conditional statements to provide more control over how
statements are processed. Labels often are used with
break
and
continue
statements
in order to break off or continue a program loop. The syntax to reference a label in such
cases is simply
break
label
;
or
continue
label
;
For example, the following
for
loop uses a statement label not only to jump out
of the programming loop when the text string
Valdez
is found, but also to jump to the
location in the script identified by the
next_report
label and to continue to process the
statements found there:
for (var i = 0; i < names.length; i++) {
if (names[i] == “Valdez”) {
document.write(“Valdez is in the list”);
break next_report;
}
}
next_report:
JavaScript statements