Web Programming
Web Programming
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
= += -= *= /= #=
== ===
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
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!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
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
! 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>