Statements :
A statement is setting a variable equal to a value. A statement is also a function call, i.e. document.write(). Statements define what the script will do and how it will be done.A Javascript code
consists of Javascript statements. Each statement is a set of expressions. JavaScript
statements should be terminated with a semicolon (;). Statements are executed
sequentially unless altered by some statements which test a condition and branch
or loop according to the result.
Javascript statements
can be categorized as :
Comments: single-line
(//) and multiline (/*...*/)
Loop Statements:
for, while, do while, labeled, break, and continue (labeled is not itself a
looping statement, but is frequently used with these statements)
Conditional statements:
if...else, and switch
Object Manipulation
Statements and Operators: for...in, new, this, and with
Conditional StatementsConditional statements are used to control your scripts so that different actions can be taken depending on the situation. Suppose you may want to display a special message during the holidays. This condition would depend on what day it was, and if it was a holiday, then a special holiday message would be displayed to your visitors. Without proper knowledge of the conditional statement your scripts will not be as lively or dynamic as they could possibly be.
(1) if...else Statement
General Format:
if (condition){
blockstatements1
}
else{
blockstatements2
}
If the condition
evaluates to true then the block of blockstatements1
is executed. Otherwise the block of blockstatements2
is executed. The else clause is optional if there are no statements which need
to be executed if the condition is false. Also, there could be only 1 statement
under an if clause.
Example1:
if (Age >= 21){
message = "Age = " + Age;
alert("Hey ! I am an adult !" + message);
}
else{
alert("Hey ! I am a Teenager !");
}
Example2:
if (Age > 21){
message = "Age = " + Age;
alert("Hey ! I am an adult !" + message);
}
Top
(2) Switch Statement
The syntax is :
switch
(expression){
case label1 :
statement;
break;
case label2 :
statement;
break;
...
default :
statement;
}
How switch works:
- First the expression
is evaluated
- Then program
looks for a label matching the value of expression and executes the associated
statement. The optional break statement associated with each case label makes
sure that the program breaks out of switch once the matched statement is executed
and continues execution at the statement following switch. If break is omitted,
the program continues execution at the next statement in the switch statement.
- If no matching
label is found, the program looks for the optional default statement, and
if found, executes the associated statement.
- If no default
statement is found, the program continues execution at the statement following
the end of switch.
eg.
switch
(Entertainment){
case "Movies" :
alert('Movies');;
break;
case "Music" :
alert('Music');;
break;
case "MTV" :
alert('MTV');;
break;
default :
alert('None of above');;
}
In
the above example, if Entertainment evaluates to "Movies", the program
matches the value with case "Entertainment" and executes the associated statement.
Since there is a break statement,program terminates switch and executes the
statement following switch.
Top
Loop Statements A loop statement checks to see if some condition is true, and if that condition is true it will execute a chunk of code.
(1) for Statement
The syntax is:
for
(initial-expression;condition-expression;increment-expression)
{ statements;}
A for loop
repeats until the condition-expression evaluates to false. Initial-expression
is executed first.Then the condition-expression is evaluated. If the
value of condition-expresion is true, the loop statements are executed.
If the value of condition is false, the for loop terminates. Then
increment-expression is evaluated and then the loop starts again.
Eg.
for
(var Age=0; Age < 100; Age++)
{document.write("Age= " + Age) }
Top
(2)do...while
Statement
The do...while
statement repeats until a specified condition evaluates to false.
The syntax is:
do
{ statements}
while (condition)
eg.
Age=0
do{
Age = Age +1 ;
document.write(Age);
}
while (Age<100);
the do loop iterates
at least once, even if initial condition is false and repeats until Age
is no longer less than 100.
Top
(3)while Statement
The syntax is:
while
(condition)
{ statements}
A while statement
executes its statements as long as a specified condition evaluates to true.
The while loop will not execute the statements even once if the condition
is initially false.
Top
(4)Labeled Statement
The labeled statement
looks as follows:
label
:
statements
Eg:
label1:
if (4==i) {
document.write("You've entered " + i + ".<BR>");
label2 :
if (2==j) {
document.write("You've entered " + j + ".<BR>");
break label2;
document.write("The sum is " + (i+j) + ".<BR>");
}
document.write(i + "-" + j + "=" + (i-j) + ".<BR>");
}
A statement labeled label1 contains a statement
labeled label1. If break is encountered, the program breaks out of the
label2 statement and continues with the remainder of the label1
statement. If break had a label of label1, the program would break out
of the label1 statement and continue at the statement following label1.
Top
(5) break Statement
The break statement can be used in a while, for, and labeled statement. This
stops the execution of the loop, drops out of loop to the next statement following
the loop.
The syntax is:
break
(In a while or for statement)
break label (In
a labeled statement)
Eg:
function
ExampleFunction() {
var Age = 0
while (Age < 1000) {
if (Age == 100) break;
Age++
}
alert('out of loop');
}
The
break statement terminates the while loop when (Age
= 100) , and then executes alert('out
of loop').
Top
(6) Continue
Statement
The continue statement
can be used in a while, for, and labeled statement. Continue statement terminates
the current loop and continues execution of the loop with the next iteration
(if condition is still true). ie., In a while loop, it jumps back to
the condition. In a for loop, it jumps to the increment-expression. In
a labeled statement it continues the defined labeled statement.
Eg1: Continue in
a While Loop
Age
= 0
x=0
while (Age <= 100) {
Age++
// when Age=60 , continue is executed and
control goes to statement Age++
if (Age == 60) continue;
//
This statement will be skipped if Age=60
document.write(Age + "<BR>");
}
The above while loop prints all ages except for 60.
Eg2:
Age=0
label1 :
while (Age < 100) {
document.write(Age + "<BR>");
Age += 1;
label :
while (x>4) {
document.write(x + "<BR>");
x-=1;
if ((x%2)==0);
continue label;
document.write(x + " is odd.<BR>");
}
document.write("Age = " + Age + "<br>");
document.write("j = " + j + "<Br>");
}
The statement labeled
label1 contains a statement labeled label. When continue
is encountered, the program terminates the current iteration of label2
and begins the next iteration. Each time continue is encountered, label
reiterates until its condition returns false. When false is returned, the remainder
of the label1 statement is completed, and label1 reiterates until
its condition returns false. When false is returned, the program continues at
the statement following label1. If continue had a label of label1, the
program would continue at the top of the label1 statement.
Top
Object manipulation
(1)for...in
The for...in statement iterates over all the properties of an object.
For each distinct property, JavaScript executes the specified statements.
The syntax is:
for (variable in object)
{ statements }
Eg:
The following function iterates through all the object's
properties and returns a string that lists the property names and their values.
Function
DisplayProperties(Object, ObjectName) {
for (var eachproperty in Object)
{
document.write(ObjectName
+ "." + eachproperty + " = " + Object[eachproperty] + '<BR>');
}
}
A complete example
is given below:
<head>
<script>
function DisplayProperties(Object, ObjectName) {
for (var eachproperty in Object)
{
document.write(ObjectName
+ "." + eachproperty + " = " + Object[eachproperty] + '<BR>');
}
}
employee=new
Object();
employee.name="Bob";
employee.sex="male";
employee.country="US";
DisplayProperties(employee,
'employee')
</script>
</head>
Result
of above program:
employee.name = Bob
employee.sex = male
employee.country = US
Top
(2)with Statement
The syntax is:
with (object)
{ statements}
The with
statement establishes the default object for a set of statements. Within the
set of statements, any property references that do not specify an object are
assumed to be for the default object.
eg:
<head>
<script>
employee=new Object();
employee.name="Bob";
employee.sex="male";
employee.country="US";
//
This is the with statement which acts as the default object
with (employee) {
document.write(name + "/" + sex + "/" + country);
}
</script>
</head>
Result
of above program:
Bob/male/US
The with
statement specifies that the employee object is the default object. The
statements following the with statement refer to properties name,sex
and country without specifying the object, employee. It is assumed
that employee object be used for these references.
Top
Comments
Comments are used
in a javascript code to explain what a script or function does. Using
comments in a Javascript code is recommended very much as this could be useful
for others to understand the logic behind each script.
JavaScript
ignores comments. There are two different ways to give comments.
Comments on a
single line are preceded by a double-slash (//). Eg.//
This is a single-line comment.
Comments that
span multiple lines are preceded by /* and followed by */:
Eg. /* This is a multiple-line comment. It can be of any
length, and you can put whatever you want here. */
A
Complete example:
<head>
<script>
var Age=0; // initialize variable Age
var Country="USA"; // Initialize variable Country
/*
Display Country */
alert("country="+ Country)
// Display Age
alert("Age="+ Age)
</script>
</head>
Top