Free Guides

Language Tutorials


           

 

JavaScript

          Index         

Your Ad Here

 

JavaScript loop structure & basics

 

JavaScript if else

If else conditional statement is very common in JavaScript. This logical condition checking is done inside a script as per the requirement. Here is the syntax for if else condition in JavaScript

If(condition){
Code;
}else{
Code;
}

The first block is executed if the condition is TRUE and second block inside the else condition is executed if the condition is FALSE.

 

Let us learn one practical example and you can see this in action at confirm window tutorial. Here is the code.
 

var my_string = confirm("Are you satisfied with the quality of the tutorials here?");
if(my_string){alert("Thank you");
}else{
alert("Please use the contact us page to give your feedbacks");}

 

JavaScript for loop & break statement inside the loop

Like any other language JavaScript for loop is one of the important built in functions to develop scripts. For loop in JavaScript is similar to for loop in C or PHP language. Other scripting languages have also similar For loops like ASP for Next loop, PHP for loop etc.

Different types of Loops are used to execute different part of the code in different logics and conditions. Some time loops are recursively use part of a code for a number of times with conditional checking. We will learn different types of loops and their uses in this section.
For loop in client side JavaScript has three parts in its declaration. The first part initialize a variable, the second part checks the condition and the third part gives the steps in which the variable will change value. Here is the syntax.
 

For( initialization; condition; step ; ){
Statement
}

 

We can print number 1 to 10 by using for loop.
 

for (var x = 1; x <= 10; x++)
{ document.write (x+"<br>");
}

 

In the above code we have added one line break after each step of printing of number so the numbers will be displayed vertically. Now let us try to display numbers starting from 10 to 0
 

for (var x = 10; x >= 0; x--)
{
document.write (x+"<br>")
}

 

In the first example x value is increased by 1 starting from 1 and in second example x value is decreased by 1 starting from 10

We can come out of the loop by using break statement inside the loop. Let us use one if condition to break the loop once x value reaches 5 . Here is the code with break statement.

 

for (var x = 1; x <= 10; x++)
{
document.write (x+"<br>");
if(x >5){break;}
}

The above code will display number 1 to 6 vertically. You can see once the value of x reaches 6 the if condition returns true and breaks the for loop.

While loop and do loop

While loops are conditional loops where a condition is checked at the starting of the loop and if the condition is true then the statements inside the loop is executed. So the changes required in condition has to be inside the statement, otherwise the loop may go for infinite looping. For easy understanding we can say While True Loop. This means as long as the expression is turn go for the loop.

You can also see PHP while loop, ASP will loop which have more or less similar purpose and to some extent similar syntax.
 

Here is the basic syntax for while loop in JavaScript.
 

While ( expression )
{
statements;

}

 

Note that here the expression is checked or evaluated before starting of the loop so if the condition returned is FALSE then the loop will never be executed.

Let us try with this example which prints

var i=0;
while (i <= 5)
{
document.write(i+"<br>")
i++;
}

 

The above code will print 0 to 5 with a line break after each number.

We can use break statement inside a while loop to come out of the loop. Here it is
 

var i=0;
while (i <= 5)
{
document.write(i+"<br>")
if(i>2){break;}
i++;
}

 

Do While Loop
Do While loop is little different than while loop. Here the condition is checked at the end of the loop. So even if the expression is FALSE then also once the statements inside the loop will be executed. This is the basic difference between do while loop and while loop. Here is an example of Do While loop in JavaScript.
 

var i=0;
do
{
document.write(i+"<br>")
i++;
} while (i <= 5)

 

In the above code condition is checked at the end of the loop only. Here also we can use break statement to come out of the loop. Here is the example
 

var i=0;
do
{
document.write(i+"<br>")
if(i>2){break;}
i++;
} while (i <= 5)

 

JavaScript Switch statement

Switch statement is used as an alternate to multiple if .. else statements. When we know various or more possibilities are to be tested then switch statement is an efficient way to do coding.

Switch statement is used in different requirements and is similar to many PHP switch and ASP switch statements.

The syntax is like this
 

switch(expression)
{
case value:
expression;
break;
 

case value:
expression;
break;

default:
Expression;

}

 

Now let us try with an example
 

var i=2;
switch (i)
{
case 1:
document.write ("value of i = 1");
break;

case 2:
document.write ("value of i = 2");
break;

case 3:
document.write ("value of i = 3");
break;

default:
document.write ("value of i is not equal to any given values");
break;
}

Now we will using switch statement display the today's day. To know the day we will use getDay() function. Here it is

var my_day=new Date()
var today=my_day.getDay();

switch (today)
{
case 0:
document.write ("Today is Sunday");
break;
case 1:
document.write ("Today is Monday");
break;

case 2:
document.write ("Today is Tuesday");
break;

case 3:
document.write ("Today is Wednesday");
break;

case 4:
document.write ("Today is Thursday");
break;

case 5:
document.write ("Today is Friday");
break;

case 6:
document.write ("Today is Saturday");
break;

default:
document.write ("value of i is not equal to any given days");
break;
}