Pertemuan 3
If statement
If else
If , else if, else statement
Switch statement
if (condition)
{
var d = new Date();
var time = d.getHours(); code to be executed if condition is true
if (time Good morning");
}
This example demonstrates the If statement.
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
if (condition)
{
var d = new Date();
var time = d.getHours();
code to be executed if condition is true
if (time Good morning");
} {
else
{ code to be executed if condition is not true
document.write("Good day");
} }
This example demonstrates the If...Else statement.
If the time on your browser is less than 10,
you will get a "Good morning" greeting.
Otherwise you will get a "Good day" greeting.
if (condition1)
{
var d = new Date(); code to be executed if condition1 is true
var time = d.getHours(); }
if (timeGood morning"); {
}
else if (time>=10 && timeGood day");
} else
else {
{
document.write("Hello World!"); code to be executed if condition1 and condition2 are not true
} }
This example demonstrates the if..else if...else statement.
switch(n)
{
var d = new Date(); case 1:
theDay=d.getDay();
switch (theDay) execute code block 1
{ break;
case 5:
document.write("Finally Friday"); case 2:
break; execute code block 2
case 6:
document.write("Super Saturday"); break;
break; default:
case 0:
document.write("Sleepy Sunday"); code to be executed if n is different from case 1 and 2
break; }
default:
document.write("I'm really looking forward to this weekend!");
}
This JavaScript will generate a different greeting based on what day it is. Note that Sunday=0, Monday=1, Tuesday=2, etc.
Buat program JavaScript yang akan melaporkan ;
Nama hari ini
Tanggal hari ini
Waktu sekarang
▪ Pagi
▪ Siang
▪ Sore
▪ Malam