Tuesday, December 14, 2010

Learning JavaScript tutorial – JavaScript Statements

A JavaScript program is simply a sequence of one or more JavaScript statements. JavaScript Statements is used to define the logic to make something happen. A statement can be used to declare a variable and assign a value. A statement can also be a function call, i.e. document.write(). JavaScript Statements are separated from each other with semicolons. The semicolon is optional in JavaScript, if you place each JavaScript statement on a separate line, JavaScript allows us to leave out the semicolons. The best practice of defining JavaScript Statements is to use semicolons, everywhere at the end of the each JavaScript statement.

JavaScript Statement Blocks

JavaScript Statement Block use Curly braces – {} to group a series of JavaScript Statements together to perform some sort of operation repeatedly or once based on desire condition. Whenever a JavaScript Statement Block occurs in JavaScript program is treated as a single statement. JavaScript Statement Block plays an important role while dealing with Conditional and Loop Statements.
<script type="text/javascript">
var iCount=0;
var iSum=0;
while(iCount<=5){
 iSum=iSum+iCount;
 iCount++;
}
alert(iSum); //alert 15
</script>

Types of JavaScript Statements

Broadly there are two types of JavaScript Statements; Expression Statements and Compound Statements. In addition to standard statements types, there are (may be) different Types of JavaScript Statements based on work done like declaring a variable, assigning a new value to variable, changing a variable's value, defining or calling a function, process a condition or loop through JavaScript Statement Blocks etc.
Here, we are going to see Expression Statements and Compound Statements. You will get introduce with other kind of Statements while dealing with JavaScript Programming.

JavaScript Expression Statements

JavaScript Expression Statements are the basic building blocks of the JavaScript. With the help of JavaScript Expression Statements, we can do basic tasks like declaring a variable, assigning a new value to variable, changing a variable’s value; perform arithmetic operations, string concatenation, calling a function etc.
var iCount=0; // declaring a variable with assigned value
var iSum=0; // declaring a variable with assigned value
iSum=iSum+iCount; //assigning a new value to variable
iCount++; //perform arithmatic oprations
alert(iSum); //calling a function
window.close();//calling a function
While declaring a JavaScript Expression Statements, please take care to terminate each line of code with a semicolon.

JavaScript Compound Statements

JavaScript Compound Statements is a way to combine a number of JavaScript Expression Statements into a single Statements, or statement block. This is simply done by enclosing any number of JavaScript Expression Statements within curly braces. While executing a compound statement, the JavaScript interpreter simply executes the statements that comprise it one after another, in the order in which they are written.
If there is any kind of error (logical or syntactical) while declaring JavaScript Compound Statements, a compound statement may terminate abruptly. We can also terminate a JavaScript Compound Statements as per our program requirement or occurrence of any error with the help of a break, continue, return, or throw statement.
while(iCount<=5){
 iSum=iSum+iCount;
 iCount++;
}
</script>

No comments:

Post a Comment