When we need to execute some code only in a specified condition, then we will use if statement.
The syntax of if statement is
if (condition)
{
code to be executed if condition is true
}
One important thing is that this if will be in small letter, if we use in capital letter it will show a JavaScript error.
Let we want that if password is “11111” it will show “Password Accepted”. So, the condition will be password==11111, and the complete code will be
var password=11111;
if (password==11111)
{
document.write("<b> Password Accepted </b>");
}
</script>
When we need to execute some code if the condition is true and another code if the condition is false, then we will use if....else statement.
The syntax of if....else statement is
if (condition)
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Let we want that if password is “xyzabc” it will show “Password Accepted” otherwise it will show “You do not have permission to enter”. So, the condition will be password==11111, and the complete code will be
var password=11111;
if (password==11111)
{
document.write("<b> Password Accepted </b>");
}
else
{
document.write("<b> You do not have permission to enter </b>");
}
</script>
When we need to execute select one of many blocks of code to be executed then we will use if...else if....else statement.
Syntax of if...else if....else statement is
if (condition1)
{
code to be executed if condition1 is true
}
else if (condition2)
{
code to be executed if condition2 is true
}
else if (condition3)
{
code to be executed if condition3 is true
}
....
....
....
else if (condition n)
{
code to be executed if condition n is true
}
else
{
code to be executed if all conditions are not true
}
For an example, let we want that it will show Good Morning before 10 AM, Good Day in between 10 AM and 4 PM and all other time it will show only Welcome!. So the first condition is time<10, second condition is time>10 && time<16. So, the complete code will be
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good day</b>");
}
else
{
document.write("<b>Welcome!</b>");
}
</script>
We use switch statement when we want to execute select one of many blocks of code to be executed.
Syntax of switch statement is
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
case 3:
execute code block 3
break;
......
......
......
default:
code to be executed if n is different from all cases
}
For an example, let we ant to know what day today is. We know that 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday and 6 = Saturday. So, the complete code will be
var d=new Date();
theDay=d.getDay();
switch (theDay)
{
case 0:
document.write("Sunday");
break;
case 1:
document.write("Monday");
break;
case 2:
document.write("Tuesday");
break;
case 3:
document.write("Wednesday");
break;
case 4:
document.write("Thursday");
break;
case 6:
document.write("Saturday");
break;
default:
document.write("Is it my Holyday!");
}
</script>

