Conditional Statements

Open Source 24 Mar , 2013  


if-else-condtional-statementsYou can start building more complex JavaScript statements by using conditional code. The conditional code allows you to ask if a condition is met. In JavaScript we have the following conditional statements:

1. if statement – This statement to execute some code only if  a given condition is true,

2. if…else statement – This statement to execute some code if the condition is true and another code if the condition is false,

3. if…else if….else statement – This statement to select one of many blocks of code to be executed,

4. switch statement – This statement to select one of many blocks of code to be executed. Now let me show you some examples of conditional statements..

    perform this action;
    perform this action if the above condition is false;
var myvar1 = true;
var myvar2 = false;

if ( myvar2 ) {
    // this code will never run
    console.log( "Hey, Hows is this possible?" );

if ( myvar2 ) {
    // this code won't run
} else {
    if ( myvar1 ) {
   // THis code will RUN
    } else {
        // this code would run if myvar1 and myvar2 were both false

NOTE: Use two equal signs (==) in the condition when comparing values. One equal sign (=) is used to assign values, while two equal signs (==) are used to compare value.

Ternary Operator, Makes your Code More Interesting

Sometimes a variable should be set depending on some condition. An if/else statement works, but in many cases the ternary operator is more convenient. The ternary operator tests a condition; if the condition is true, it returns a certain value, otherwise it returns a different value.

// set myvar1 to 1 if myvar2 is true;
// otherwise, set foo to 0
var myvar1 = myvar2 ? 1 : 0;

While the ternary operator can be used without assigning the return value to a variable, this is generally discouraged.

Switch Statements :

These Statements are very useful sometimes when there are lots of subsequent conditions out-there . Switch statement is specifically designed for comparing one variable to a number of possible values. It can be thought of as a substitute for the if, else if, else structure. There is an important keyword used within the switch structure, and that keyword is the break keyword. The break keyword is used to make sure that the switch structure will not fall through to the next possible value, even if that value is incorrect within the switch structure.

<script type="text/javascript">
X = 7;

case 1:
    document.write("X is equal to 1");
case 2:
    document.write("X is equal to 2");
case 3:
    document.write("X is equal to 3");
case 7:
    document.write("X is equal to 7");
    document.write("X is not equal to any of the values specified");

So The out put of above will be : X  is  equal  to  7.

Instead of using switch here, if you use IF_ELSE series then Code will be very complicated and complex for other and also for you in future, if you forget to comment the objective of the code, so Sometimes Switch Statements are very easy and We all thanks to JavaScript Technology for giving us this Helpful way 🙂

, , , , , , , , ,