conditionals statements:

function password()
{

Ret = prompt('Type the word castle',"");

if(Ret=="castle")
{
   alert("Your password is valid");
   location = '../00.htm';
  }
else
{
   alert("Please try again")
}

}

 

The reserve word if/else block test condition to see if the variable Ret is equal to the value "castle"

Condition is a match
    - notify user that pass word is valid
    - load document 00.htm

else condition is not a match
     - notify user to Please try again

In JavaScript there are three conditional statements:

if statement - use this statement if you want to execute a set of code when a condition is true
if...else statement - use this statement if you want to select one of two sets of lines to execute
switch statement - use this statement if you want to select one of many sets of lines to execute

If Statement

if (condition)

if (var1 == "print")

{

{

javascript statement here

document.write ("hello world");

}

}

In the example above, the if statement compares the character string "print" to the var1 to test the condition. The next statment is a block of JS code that will execute if var1 is equal to print.

        1. Reserved word = if
        2. condition = (var1=="print") is var1 equal to print
        3. begin block of JS code = {
        4. document.write ("hello world"); "write "hello world" to the document ".
        5. End block of JS Code = }

If/Else Block

See the example used earlier to get a demonstration on how the is/else block works.

 

 

 

 

 

 

 

 

 

 

Switch Statement

The switch statement is basically an enhanced version of the "if-else" statement that is more convenient to use when you have code that needs to choose a path from many to follow.

A switch statement is essentially the same as a series of "if" and "else-if" statements. "So, what's the point of even having a switch statement", you may ask. Well, it's simple to use, logical in syntax, and most importantly, many programmers swear by it.

Source:Keywork switch

Results :

switch (favoritemovie){
case "Titanic":
alert("Not a bad choice!")
break;
case "Water World":
alert("No comment")
break;
case "Scream 2":
alert("It has it\'s moments")
break;
default : alert("I\'m sure it was great");
}

 

 
Imsg
Discussion

Blogging

E-mail

 

JAVASCRIPT    -    LESSON 3
J s - c o n d i t i o n a l