Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

23/02/2014 Categories: Javascript. No Comments on Control Statements in Javascript



Statements that are used to control the flow of execution in a script are known as control statements. Control statements are used along with compound statements (a set of statements enclosed in braces).


Local variables are not allowed in a control construct (control statement + single/compound statement). Even though a variable is declared within a control construct, it is treated as a global variable.


Control Expressions


The expressions upon which the flow of control depends are known as control expressions. These include primitive values, relational expressions and compound expressions. The result of evaluating a control expression is always a Boolean value either true or false.


Selection Statements


Statements which are used to execute a set of statements based on a condition are known as selection statements. In JavaScript selection statements are: if, if-else and switch.


Syntax for an if statement is shown below:



Syntax for an if-else statement is shown below:



Syntax of a switch statement is given below:



The expression and labels in a switch statement can be either numbers, strings or Boolean values.


Loop Statements


Statements which are used to execute a set of statements repeatedly based on a condition are known as loop statements or iteration statements. Loop statements supported by JavaScript are while, do-while, for and for-in.


Syntax of while loop is given below:



Syntax for do-while loop is given below:



In a do-while statement condition is evaluated after the body of the loop is executed. So, the difference between while and do-while is, unlike in while loop, the body of the loop is guaranteed to be executed at least once in a do-while loop.


Syntax of a for loop is given below:



JavaScript provides another loop statement to work with objects which is known as a for-in loop. It’s syntax is given below:



Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up