This example demonstrates the If statement. A simple comparison is made. Action is only specified if the condition is true.
If the time on your browser is less than 12, you will get a "Good morning" greeting.
Next the date refering to the day of the month is given.
Finally, the Conditional Operator returns its value which is either Saturday or not.
<script type="text/javascript">
var d =new Date();
var time = d.getHours();
var num_day = d.getDate();//This is the date of the month.
var day = d.getDay();//This is number for day of week.
if (time < 12)
{
document.write("Good morning");
}
day=(day==6)?"It's Saturday":"It's NOT Saturday!";//This is an coditional operator.
The calls are writen as follows between script tags:
document.write("The day of the month is: " + num_day +"."+ "<br />");
document.write(day + "<br />");
</script>
Return to Table of Contents