Use the if statement to execute some code only if a specified condition is true.
Syntax
if (condition) { code to be executed if condition is true } |
Note that if is written in lowercase letters. Using uppercase letters (IF) will generate a JavaScript error!
Example
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
{
document.write("Good morning");
}
<script type="text/javascript">
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
{
document.write("<b>Good morning</b>");
}
</script>
//Write a "Good morning" greeting if
//the time is less than 10
var d=new Date();
var time=d.getHours();
if (time<10)
{
document.write("Good morning");
}
0 comments: