If Statement
This statement is used to execute some code only if a specified condition is true.
Syntax:
{
code to be executed if condition is true
}
Example:
var d=new Date()
var time=d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>")
}
</script>
This script writes "Good morning" greeting if the time is less than 10
If .... elese statement
This statement is used to execute some code only if the condition is true and another code if the condition is false.
Syntax:
{
code to be executed if condition is true
}
else
{
code to be executed if condition is not true
}
Example:
var d=new Date()
var time=d.getHours()
if (time<10)
{
document.write("Good morning")
}
else
{
document.write("Good Day")
}
</script>
Preview:
If .... else If ..... else statement
This statement is used to select one of many blocks of code to be executed.
Syntax:
{
code to be executed if condition1 is true
}
else if {condition2}
{
code to be executed if condition2 is true
}
else
{
code to be executed if condition1 and condition2 are not true.
}
Example:
var d=new Date()
var time=d.getHours()
if (time<10)
{
document.write("Good morning")
}
else if (time>=10 && time<16)
{
document.write("Good Day")
}
else
{
document.write("Hello World")
}
</script>
Preview:
Switch statement
This statement is used to select one of many blocks of code to be executed.
Syntax:
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
Example:
var d=new Date();
theDay=d.getDay();
switch(theDay)
{
case 5:
document.write("Good Friday");
break;
case 6:
document.write("Super Saturday");
break;
case 0:
document.write("Sleepy Sunday");
break;
default:
document.write("I am looking forward to this weekend!");
}</script>
Preview:
I am looking forward to this weekend!Read Next: How to Show Pop Up Boxes Using JavaScript?
Related Search Terms
Javascript Conditional Statements
Javascript if and if else Statement
JavaScript if Condition Example
Related Posts:
How To Create Simple Image Slideshow Using JavaScript ?
Image Slideshow with Navigation Buttons Using JavaScript
How to create Changeable Date and Time Using JavaScript?
How to Create JavaScript Image Slideshow with LInks
How to Display Date Format in JavaScript?
How to Validate a HTML Form Using JavaScript?
What are the Different Ways to Redirect Page in JavaScript?
How to Detect Visitor's Browser Using JavaScript?
How to make rounded corners border using CSS
How to Create Custom CSS Template for Printing
How to create a simple form using HTML?
إرسال تعليق
Click to see the code!
To insert emoticon you must added at least one space before the code.