JavaScript Statement
JavaScript Supports The Following Types of the Statement
1. block Statement
2. break Statement
3. Continue Statement
4. do-While Statement
5. For Statement
6. For-in Statement
7. if-else Statement
8. return Statement
9. Switch Statement
10. Try..Catch Statement
11. While Statement
12. Comment Statement
1. block Statement
block statement is starts with the opening curly brace ( { ) and closed with
Closing curly brace ( } ) . We use block Statement when we wants to
Execute a set of Statement together or inside a one Loop.
Ex :
var x;
For(x=10;x<=20;x++)
{
// Here, You Write your Own Code
}
function number(){
// Your Function Code
}
2. break Statement
In JavaScript , The Function of the break Statement is Similar to the other
Programming Language Like C , C++ , Java etc. break Statement is used
To terminates the Set of Instructions , Conditions , and Different types of
Loop like for , While , do...While etc.
Ex :
var x;
for(x=0;x<10;x++)
{
if(x<5){
break;
}
document.write("<h1> Example of break Statement </h1>");
}
3. Continue Statement
Continue Statement is used to bypass the loop so that it will not execute the
Same loop further and go to the next iteration.
Ex :
var i;
for(i=10;i<20;i++)
{
if(i<15){
continue;
}
document.write("<h1> Example of Continue Statement </h1>");
}
4. do..While Statement
do..While Statement is the same as the Other loop Statement Like For , While
but only difference that do..While Statement is Execute at least one time
because in this Statement, condition is checked after the body part .
Syntax :
// initialization Part
do
{
// body Part
// Changing Part
}
While(condition Part)
Ex :
var a=0;
do
{
document.write("do..While Loop" +a+ "<br/>");
a=a+2;
}
while(a<10);
5. For Statement
Every Loop Statement is Contains The Three Parts..
(A). initialization Part
(B). Condition Part
(C). Changing Part
Syntax :
for( initialization ; Condition ; Changing )
{
// body Part
}
Ex :
var i;
For(i=20;i<=30;x=x+2)
{
// Here, You Write your Own Code
}
6. For..in Statement
for..in Loop is Similar to the For..in Loop used in The Java. This Loop Execute
The code For all the items in loop or array . it will Take two parameters as
Argument
(A). variable For the Specified Array
(B). Specify The Array
Ex :
var Country=new Array("India" , " China" , "Pakistan" , "Sri lanka" , "Japan");
var name;
for (name in Country)
{
document.write(Country[name] + "<br/>");
}
7. if..else
This Condition Statement is used To Check the Condition . if Condition is True
it will execute the code inside the if loop Otherwise it will execute the condition
of the else loop. you can use a nested if..else loop also but nesting should be in
Proper Order.
Syntax :
if(Condition)
{
// Code of if Loop
}
else
{
// code of else Loop
}
Ex :
var x=24;
var y=35;
if(x>=y)
{
document.write("variable x is bigger than variable y");
}
else
{
document.write("variable y is bigger than variable x");
}
8. Return Statement
return statement is used to return the value from any function.
Ex :
function Addition (a,b)
{
return a+b;
}
var add=Addition(40,50);
document.write("Addition of the given two number is " + add);
9. Switch Statement
Switch Statement is the Replacement of the Long Nesting of the if..else ,
if..else Loop. it Evaluates argument Pass to it and Executes the
Corresponding Case Code . if Any Corresponding Case label not Found
Than it Will Execute the default Code.
Ex :
var browser="Chrome"
switch (browser)
{
case "Firefox" :
document.write(" Specified browser is Firefox");
break;
case "Chrome" :
document.write(" Specified browser is Chrome");
break;
case "Opera" :
document.write(" Specified browser is Opera");
break;
default :
document.write("Specified browser is not matched with the Firefox , chrome and Opera");
}
10. Try..Catch Statement
Similar to the try..catch block used in C++ , Java etc. it is used to handle
The Error of the Codes . Try block checks the Error and Throw it . if
Catch block finds the similar type of error , Specified as Argument , it
Will catch it and Execute the Corresponding Code. You Can Add a
Multiple Catch block also .
Ex : divide by Zero
var a=10;
var b=5;
var c=5;
try
{
var z=a/(b-c);
}
catch(e)
{
document.write("divide by Zero Error");
}
11. While Statement
While Statement is Execute The body Code if condition is true Otherwise
Terminates The Loop.
Syntax :
// initialization Part
While(condition Part)
{
// body Part
// Changing Part
}
Ex :
var a=10;
while(a<10)
{
document.write("While Loop" +a+ "<br/>");
a=a+2;
}
12. Comment Statement
Comment Statement ( // ) is Used To Tell The JavaScript Browser that You
not Want to Execute This Line of Code in your Source Program so do not
Compile it.
Ex :
// var a;
// document.write("Comment Line Example");
Thank You To All My Reader
Deepak Gupta
www.i-world-tech.blogspot.in
Related Post
1. Mini Project in C : Book Shop Inventory System
2. How to Optimize images for Better Search Engine Ranking
3. What are noFollow and doFollow Links ?
4. Java Script Basics : POP UP BOXES
5. Simple Calculator Program in JavaScript
6. Introduction to JavaServer Faces - JSF
7. Shell Programming : Program to generate all the combination of 1,2 and 3 using For Loop
8. Batch programming :- How we Create a simple game in batch programming
9. Batch Programming :How To make a Simple Calculator in Batch programming
10. Multimedia Synchronization
nice tutorials..10nx 4 shring.. :) :)
ReplyDeletegud article..10nx 4 sharing..:)
ReplyDelete