Conditional Statements
If
You use the if statement in the following syntax:
if (condition) {
statements
}
The condition can be any logical expression. if the result of condition is true, the statements enclosed in curly braces are executed and program execution continues. If the condition returns false, JavaScript ignores the block of code between the curly braces and continues thereafter. It is common practice to indent the set of statements enclosed in curly braces. This helps keep a logical look to your scripts.
If…Else
Sometimes using the if statement alone is not enough. You can also reserve a set of statements to execute if the conditional expression returns false. This is one by adding an else block of statements immediately following the if block. The syntax follows:
if (condition) {
statements
} else {
statements
}
Looping

A looping structure.
Creating a loop inside a script can serve many purposes. One simple but common use of a loop is counting. For example, to display the numbers from 0 to 9, you could write
document.writeln("0")
document.writeln("1")
…
document.writeln("9")
This works fine, but what if you needed to count till 1000? You can do it in the same manner, but it takes much more time and increases the time needed to download the page. The best way to do it would be using a loop.
For
You use the for statement to start a loop in a script. The following is the syntax:
for ([initializing_expr]; [condition_expr]; [loop_expr]) {
statements
}
The three expressions enclosed are optional, but if you omit one, the semicolons are still required. This keeps each expression in its appropriate place. You typically use the initializing expression must evaluate to true before each execution of the statements enclosed in curly braces.
A typical use of a loop follows:
<script language = "JavaScript">
for (var i = 0; i < 100; ++i) {
if (i%10 == 0) {
document.writeln()
}
document.write(i + " ")
}
The code will loop and increment i, displaying the value of i from 0 to 99 and starting a new line after every 10 numerals.
While
The statement while acts similarly to a for loop but does not include the function of initializing or incrementing variables in its declaration. You must declare variables beforehand and increment or decrement the variables within the statements block.
For example:
while (i < 10) {
document.write(i)
++i
}