0% found this document useful (0 votes)
45 views

Web Programming

1. The document discusses various JavaScript operators including arithmetic, assignment, comparison, logical, and string operators. It also covers conditional statements like if, else if, else and switch statements. 2. Examples are provided to demonstrate how to use if/else statements to write different greetings based on the time of day. A switch statement is used to write different greetings for different days of the week. 3. The last example uses an if statement to randomly display one of two links based on the output of a random number.

Uploaded by

DeSsy SaMe
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Web Programming

1. The document discusses various JavaScript operators including arithmetic, assignment, comparison, logical, and string operators. It also covers conditional statements like if, else if, else and switch statements. 2. Examples are provided to demonstrate how to use if/else statements to write different greetings based on the time of day. A switch statement is used to write different greetings for different days of the week. 3. The last example uses an if statement to randomly display one of two links based on the output of a random number.

Uploaded by

DeSsy SaMe
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

MATERI PERKULIAHAN HTML DAN JAVASCRIPT Pertemuan Minggu 10 Oleh: Mohamad Saefudin, Skom, MMSI (Operator, Conditional Statement

if, Switch)

JavaScri t ! erat"r#
Arit$metic ! erat"r#
Operator + * / # Pengertian Addition Subtraction Multiplication Divi ion Modulu $Si a %&'ba(ian) +ncr&'&nt D&cr&'&nt Contoh x=2, y=2, x+y x=5, y=2, x-y x=5, y=4, x*y !5/5 5/2 5#2 !0#* !0#2 x=5 x++ x=5 x-Hasil 4 3 20 3 2"5 ! 2 0 x=, x=4

++ --

A##ignment ! erat"r#
Operator Contoh Hasil

= += -= *= /= #=

x=y x+=y x-=y x*=y x/=y x#=y

x=y x=x+y x=x-y x=x*y x=x/y x=x#y

C"m ari#"n ! erat"r#


Operator Pengertian Contoh

== ===

i &-ual to i &-ual to $c/&c0 .or bot/ valu& and typ&)

5==* r&turn .al & x=5 y=151 x==y r&turn tru& x===y r&turn .al &

2= 3 4 3= 4=

i not &-ual i (r&at&r t/an i l& i l& t/an t/an or &-ual to i (r&at&r t/an or &-ual to

52=* r&turn tru& 53* r&turn .al & 54* r&turn tru& 53=* r&turn .al & 54=* r&turn tru&

L"gica% ! erat"r#
Operator 55 Description And Example x=, y=3 $x 4 !0 55 y 3 !) r&turn tru& x=, y=3 $x==5 66 y==5) r&turn .al & x=, y=3 2$x==y) r&turn tru&

66

7r

8ot

String ! erat"r String adalah kumpulan dari text, sebagai contoh "Hello World!" menggabungkan dua atau lebih sting kita bisa gunakan operator "
txt1="What a very" txt2="nice day!" txt3=txt!+txt2
variabl& t&tx3 b&rnilai 9 1:/at a v&rynic& day21"

!ntuk

!ntuk menambahkan spasi di antara dua atau lebih #asiabel string


txt1="What a very" txt2="nice day!" txt =txt1!" "!txt2 or txt1="What a very " txt2="nice day!" txt3=txt!+txt2
variabl& t&xt3 b&rnilai9 1:/at a v&ry nic& day21"

C"n&iti"na% ! erat"r

$a#aScript also contains a conditional operator that assigns a #alue to a #ariable based on some condition
Sinta' varia"lename=(condition)#val$e1%val$e2

C"nt"$ &reetin&=(vi'itor=="()*S")#"+ear (re'ident "%"+ear "


+. t/& variabl& vi itor i &-ual to %;<S, t/&n put t/& trin( 1D&ar %r& id&nt 1 in t/& variabl& na'&d (r&&tin(" +. t/& variabl& vi itor i not &-ual to %;<S, t/&n put t/& trin( 1D&ar 1 into t/& variabl& na'&d (r&&tin("

C!NDITI!NAL STATEMENTS I( % i) #tatement & use this statement if 'ou (ant to execute some code onl' if a specified condition is true ) i)***e%#e #tatement & use this statement if 'ou (ant to execute some code if the condition is true and another code if the condition is false * i)***e%#e i)****e%#e #tatement & use this statement if 'ou (ant to select one of man' blocks of code to be executed + #+itc$ #tatement & use this statement if 'ou (ant to select one of man' blocks of code to be executed I( STATEMENT
Syntax if (condition) , code to be executed if condition is true Example 1 .'cript type="text/0ava'cript"1 //Write a "2ood mornin&" &reetin& if //the time i' le'' than 13 var d=new +ate() var time=d4&et5o$r'() if (time.13) , doc$ment4write("."12ood mornin&./"1") ./'cript1 Example 2 .'cript type="text/0ava'cript"1 //Write "6$nch7time!" if the time i' 11 var d=new +ate() var time=d4&et5o$r'() if (time==11) , doc$ment4write("."16$nch7time!./"1") ./'cript1

I(***ELSE STATEMENT
S,ntaif (condition) , code to be executed if condition is true el'e , code to "e exec$ted if condition i' not tr$e E-am %e .'cript type="text/0ava'cript"1 //8f the time i' le'' than 13, //yo$ will &et a "2ood mornin&" &reetin&4 //Otherwi'e yo$ will &et a "2ood day" &reetin&4 var d = new +ate() var time = d4&et5o$r'() if (time . 13) , doc$ment4write("2ood mornin&!") el'e , doc$ment4write("2ood day!") ./'cript1

I(***ELSE I(***ELSE STATEMENT


S,ntaif (condition1) , code to be executed if condition1 is true el'e if (condition2) , code to be executed if condition2 is true el'e , code to be executed if condition1 and condition2 are not true E-am %e .'cript type="text/0ava'cript"1 var d = new +ate() var time = d4&et5o$r'() if (time.13) ,

doc$ment4write("."12ood mornin&./"1") el'e if (time113 99 time.1:) , doc$ment4write("."12ood day./"1") el'e , doc$ment4write("."15ello World!./"1") ./'cript1

C!NDITI!NAL STATEMENTS S.ITCH


S,nta'witch(n) , ca'e 1% execute code block 1 "rea; ca'e 2% execute code block 2 "rea; defa$lt% code to be executed if n is different from case 1 and 2 E-am %e .'cript type="text/0ava'cript"1 //<o$ will receive a different &reetin& "a'ed //on what day it i'4 =ote that S$nday=3, //>onday=1, ?$e'day=2, etc4 var d=new +ate() the+ay=d4&et+ay() 'witch (the+ay) , ca'e @% doc$ment4write("Ainally Ariday") "rea; ca'e :% doc$ment4write("S$per Sat$rday") "rea; ca'e 3% doc$ment4write("Sleepy S$nday") "rea; defa$lt% doc$ment4write("8Bm loo;in& forward to thi' wee;end!") ./'cript1

Example <html> <body> <script type="text/javascript"> var r=Math.random() if (r>0. )

! doc"ment.#rite("<a href=$http%//###.#&schools.com$>'earn (eb )evelopment*</a>") + else ! doc"ment.#rite("<a href=$http%//###.refsnesdata.no$>,isit -efsnes )ata*</a>") + </script> </body> </html>

You might also like