01 JavaScript
01 JavaScript
JavaScript
• Menyediakan fasilitas pemrograman untuk lingkungan browser web
• Membuat web menjadi lebih dinamis dan interaktif
– aplikasi bereaksi terhadap suatu event dalam halaman web
– membaca dan menulis ulang tag HTML
– memvalidasi data yang akan dikirimkan ke server
• Didukung oleh banyak (tidak semua) browser web
• Tidak membutuhkan tool khusus, dapat mempergunakan editor teks biasa
seperti halnya pada saat membuat dokumen HTML
• JavaScripts ≠ Java
– JavaScripts dikembangkan Netscape Æ script
– Java dikembangkan Sun Microsystems Æ applet
• Variant
– ECMAScripts
– JScript
Program JavaScript
• Berupa script/program yang disisipkan di dalam dokumen
HTML
• Peletakan JavaScript
– Dalam internal dokumen HTML
• Standar HTML 4.01
<script type="text/javascript">
…statement…
</script>
• Tag Lama
<script language="JavaScript">
…statement…
</script>
– Dalam file external
• Script JavaScript dituliskan pada file tersendiri (ekstensi file .js)
• Pemanggilan file JavaScript:
<script src="namafile.js"></script>
Tipe Data
• Numerik
– Bilangan: integer, float
• String
– Karakter, angka, tanda baca
• Boolean
– Nilai 0 untuk false
– Bukan 0 untuk true
Variabel
• Penamaan
– Case sensitive
– Harus diawali dengan huruf atau underscore
– Bukan keyword JavaScript
– Tidak mengandung karakter tanda baca atau karakter untuk
operasi pada script
• Scope
– Global (default)
• Dapat diakses oleh bagian script mana pun di dalam dokumen
– Lokal
• Dideklarasikan di dalam fungsi
• Hanya dapat diakses oleh fungsi yang mendefinisikannya
• Deklarasi
var namavariabel=nilai (lokal)
namavariabel=nilai (global)
Operator
• Operator Aritmetika
• Operator Assigment
• Operator Pembandingan
• Operator Logika
• Operator String
• Operator Kondisi
• Operator new
Operator Aritmetika
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
%= x%=y x=x%y
Operator Pembandingan
Methods
• charAt(index) Returns the character at the location specified by index.
• indexOf(findString,startingIndex) Returns the index of the first occurrence of
findString, starting the search at startingIndex where startingIndex is optional-if it is
not provided, the search starts at the start of the string.
• lastIndexOf(findString,startingIndex) Returns the index of the last occurrence of
findString. This is done by searching backwards from startingIndex. startingIndex is
optional and assumed to be the last character in the string if no value is provided.
• split(separator) Returns an array of strings created by splitting the string at every
occurrence of separator.
• substring(firstIndex,lastIndex) Returns a string equivalent to the substring
starting at firstIndex and ending at the character before lastIndex. If firstIndex is
greater than lastIndex, the string starts at lastIndex and ends at the character before
firstIndex.
• toLowerCase() Returns a string containing the value of the String object with all
character converted to lowercase.
• toUpperCase() Returns a string containing the value of the String object with all
character converted to uppercase.
Contoh String Object
<html>
<script type="text/javascript">
str = "Sangkuriang is from Bandung";
alert(str.length); //27
alert(str.charAt(4)); //k
alert(str.indexOf("from")); //15
alert(str.lastIndexOf("an")); //21
alert(str.split(" ")); //Sangkuriang,is,from,Bandung
alert(str.substring(6, 11)); //riang
alert(str.toLowerCase()); //sangkuriang is from bandung
alert(str.toUpperCase()); //SANGKURIANG IS FROM BANDUNG
Methods
• join(string) Returns a string containing each element of the array separated by
string.
• reverse() Reverses the order of an array.
• sort(function) Sorts an array based on function which indicates a function defining
the sort order. function can be omitted in which case the sort defaults to dictionary
order.
Contoh Array (menggunakan indeks)
<html>
<script type="text/javascript">
//cara 1 pendefinisian array
mhs = new Array();
mhs[0] = "Bevin";
mhs[1] = "Andini";
mhs[2] = "Citra";
//cara 2 pendefinisian array
mhs = new Array("Bevin", "Andini", "Citra");
//cara 3 pendefinisian array
mhs = ["Bevin", "Andini", "Citra"];
</script>
</body>
</html>
Math Object
Properties
• E The value of Euler's constant (roughly 2.718) used as the base for natural logarithms.
• LN10 The value of the natural logarithm of 10 (roughly 2.302).
• LN2 The value of the natural logarithm of 2 (roughly 0.693).
• PI The value of pi-used in calculating the circumference and area of circles (roughly 3.1415).
• SQRT1_2 The value of the square root of one-half (roughly 0.707).
• SQRT2 The value of the square root of two (roughly 1.414).
Methods
• abs(number) Returns the absolute value of number.
• sin(number), cos(number), tan(number), asin(number), acos(number), atan(number)
Standard trigonometric functions with number in radians.
• atan2(number1,number2) Returns the angle of the polar coordinate corresponding to the
Cartesian coordinate (number1, number2). [Not I]
• ceil(number) Rounds up to the next integer.
• exp(number) Returns the value of e to the power of number.
• floor(number) Rounds down to the nearest integer.
• log(number) Returns the natural logarithm of number.
• max(number1,number2) Returns the greater of number1 and number2.
• min(number1,number2) Returns the smaller of number1 and number2.
• pow(number1,number2) Returns the value of number1 to the power of number2.
• random() Returns a random number between zero and one.
• round(number) Rounds to the closest integer.
• sqrt(number) Returns the square root of number.
Contoh Math Object
<html>
<script type="text/javascript">
</script>
</body>
</html>
Date
• Object date dapat digunakan untuk mengeset, mengambil, dan
memanipulasi tanggal dan waktu
• JavaScript menyimpan Date sebagai angka integer berupa banyaknya
milidetik sejak 1 Januari 1970, pukul 00:00:00. Tanggal sebelum itu
tidak dapat diakses
• Pembuatan object Date
obj_name = new Date();
obj_name = new Date(string_date);
format of string_date = "month date, year hour:min:sec"
obj_name = new Date(list_of_integers);
list_of_integers = year, month, date[, hour, min, sec]
Date Object
Methods
• getDate() The day of the month as an integer from 1 to 31.
• getDay() The day of the week as an integer from 0 to 6 (where 0 is Sunday, 1 is Monday, etc.).
• getHours() The hours the time as an integer from 0 to 23.
• getMinutes() The minutes as an integer from 0 to 59.
• getMonth() The month as an integer from 0 to 11 (where 0 is January, 1 is February, etc.).
• getSeconds() The seconds as an integer from 0 to 59.
• getTime() The time of the current Date object as an integer representing the number of
milliseconds since January 1, 1970 at 00:00:00.
• getYear() The year as an integer representing the year minus 1900.
• getFullYear() The year as an integer the year.
• parse(dateString) The number of milliseconds between January 1, 1970 at 00:00:00 and the
date specified in dateString. dateString should take the format
Day, DD Mon YYYY HH:MM:SS TZN or Mon DD, YYYY
• setDate(dateValue) Sets the day of the month. dateValue is an integer from 1 to 31.
• setHours(hoursValue) Sets the hour. hoursValue is an integer from 0 to 23.
• setMinutes(minutesValue) Sets the minutes. minutesValue is an integer from 0 to 59.
• setMonth(monthValue) Sets the month. monthValue is an integer from 0 to 11.
• setSeconds(secondsValue) Sets the seconds. secondsValue is an integer from 0 to 59.
• setTime(timeValue) Sets the value for the current Date object. timeValue is an integer
representing the number of milliseconds since January 1, 1970 at 00:00:00.
• setYear(yearValue) Sets the yea. yearValue is an integer greater than 1900.
• toGMTString() Returns the value of the current Date object in GMT as a string using Internet
conventions in the form Day, DD Mon YYYY HH:MM:SS GMT
Contoh Date Object
<html>
<script type="text/javascript">
//mengambil informasi waktu saat ini
hari_ini = new Date();
document.write("Tanggal = "+hari_ini.getDate() +"<br>");
//sembarang waktu
waktu = new Date("March 15, 2004 17:04:22");
document.write("Tanggal = "+waktu.getDate() +"<br>"); //15
document.write("Bulan = "+waktu.getMonth() +"<br>"); //2
document.write("Tahun = "+waktu.getYear() +"<br>"); //2004
document.write("Hari = "+waktu.getDay() +"<br>"); //1 (Monday)