Hai programmmer… Kali ini kita akan melanjutkan pembahasan kita sebelumnya tentang Pengenalan Javascript Lengkap. Silahkan dibaca terlebih dahulu karena artikel ini sangat berkaitan dengan artikel pembelajaran javascript part-1 sebelumnya. Kita akan coba memahami apa itu javascript output, statement, syntax juga comment, sehingga nantinya kita akan lebih mudah lagi memahami materi selanjutnya tentang Tutorial Javascript Indonesia.
Artikel pembelajaran kali ini sudah saya buat terpisah dengan harapan memudahkan kita memahami dasar-dasar javascript yang wajib kita ketahui. Silahkan anda simak dan perhatikan dengan seksama pembahasan javascript di bawah ini yang menjelaskan tentang Output, Statement, Syntax dan Comments pada javascript.
Javascript Output
Javascript dapat menampilkan data dengan bebagai cara, ialah sebagai berikut :
- Menulis menjadi elemen HTML, menggunakan innerHTML.
- Menulis ke dalam output HTML menggunakan document.write().
- Menulis ke dalam kotak peringatan, menggunakan window.alert().
- Menulis ke konsol browser, menggunakan console.log().
Masih bingung? akan saya jelaskan satu persatu.
1. Menggunakan innerHTML
Untuk mengakses elemen HTML, JavaScript dapat menggunakan metode document.getElementById(id). Atribut id mendefinisikan elemen HTML. Properti innerHTML mendefinisikan konten HTML. Perhatikan contoh di bawah ini.
<!DOCTYPE html> <html> <body> <h1>Percobaan Pertama</h1> <p>Halaman percobaan pertama</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 + 6; </script> </body> </html>
2. Menggunakan document.write()
Sebenarnya, saat kita sedang developing aplikasi, lebih mudah menggunakan document.write() saat pengkodean aplikasi kita untuk debuging. Perhatikan contoh berikut ini.
Contoh pertama :
<!DOCTYPE html> <html> <body> <h1>Halaman pertama saya</h1> <p>Halaman pertama saya.</p> <script> document.write(5 + 6); </script> </body> </html>
Contoh kedua :
<!DOCTYPE html> <html> <body> <h1>Halaman saya</h1> <p>Halman saya</p> <button type="button" onclick="document.write(5 + 6)">Coba</button> </body> </html>
penggunaan document.write() sebaiknya digunakan hanya untuk kebutuhan testing.
3. Menggunakan window.alert()
Anda juga bisa menggunakan window.alert() untuk menampilkan data. Perhatikan contoh berikut.
<script> window.alert(5 + 6); </script>
Juga anda bisa menghilangkan window saat untuk menampilkan alert. Dalam JavaScript, alert adalah objek lingkup global, yang berarti bahwa variabel, properti, dan metode secara default adalah milik si alert. Ini juga berarti bahwa menggunakan sintax window adalah opsional. Perhatikan contoh berikut :
<script> alert(5 + 6); </script>
4. Menggunakan console.log()
Saat anda developing, anda bisa menggunakan console.log() untuk debugging sehingga bisa menampilkan data pada browser. Perhatikan contoh berikut ini :
<!DOCTYPE html> <html> <body> <script> console.log(5 + 6); </script> </body> </html>
Javascript Statements (Pernyataan Javascript)
Javascript Statements (Pernyataan JavaScript) terdiri dari Values, Operators, Expressions, Keywords, dan Comments.
Sebagian besar program JavaScript berisi banyak pernyataan JavaScript. Pernyataan dieksekusi, satu per satu, dalam urutan yang sama seperti yang tertulis. Penjelasan tentang Javascript Statements ini saya buat terpisah agar memudahkan kita mempelajari bagian-bagian dari Pernyataan Javascript tersebut.
1. Semicolons (Titik Koma)
Titik koma memisahkan pernyataan JavaScript. Tambahkan tanda titik koma di akhir setiap pernyataan yang dapat dieksekusi:
var a, b, c; // menyatakan 3 variable a = 5; // menetapkan nilai a adalah 5 b = 6; // menetapkan nilai b adalah 6 c = a + b; // menetapkan menjumlahan antara a dan b
Ketika dipisahkan oleh titik koma, beberapa pernyataan pada satu baris juga diperbolehkan. Perhatikan contoh berikut :
a = 5; b = 6; c = a + b;
2. Javascript Code Blocks
Pernyataan JavaScript dapat dikelompokkan bersama dalam blok kode, di dalam kurung keriting {…}. Tujuan dari blok kode adalah untuk mendefinisikan pernyataan yang akan dieksekusi bersama. Beberapa penggunaan pada penerapan ini, anda terkadang akan menemukan pernyataan yang dikelompokkan bersama dalam blok, dalam fungsi JavaScript tersebut. Lihat contoh berikut :
function myFunction() { document.getElementById("demo1").innerHTML = "Hai wak!"; document.getElementById("demo2").innerHTML = "Sehat kan?"; }
3. Javascript Keyword
Pernyataan JavaScript sering dimulai dengan keyword untuk mengidentifikasi tindakan JavaScript yang harus dilakukan. Berikut adalah daftar beberapa keyword yang dapat anda pelajari di tutorial kali ini.
Keyword | Description |
---|---|
break | Mengakhiri switch atau loop (perulangan) |
continue | Melompat keluar dari lingkaran dan mulai di atas |
debugger | Menghentikan eksekusi JavaScript, dan memanggil (jika ada) fungsi debugging |
do … while | Mengeksekusi blok pernyataan, dan mengulangi blok, sementara kondisi benar |
for | Menandai blok pernyataan yang akan dieksekusi, selama syaratnya benar |
function | Deklarasi function |
if … else | Menandai blok pernyataan yang akan dieksekusi, tergantung pada suatu kondisi |
return | Exits a function |
switch | Menandai blok pernyataan yang akan dieksekusi, tergantung pada kasus yang berbeda |
try … catch | Menerapkan penanganan kesalahan pada blok pernyataan |
var | Deklarasi variable |
Javascript Syntax
Sintaks JavaScript mendefinisikan dua jenis nilai: Nilai tetap dan nilai variabel. Nilai tetap disebut literal. Nilai variabel disebut variabel.
1. JavaScript Literals
Aturan paling penting untuk menulis nilai tetap adalah:
Angka ditulis dengan atau tanpa desimal:
10.50 1001
String adalah teks, ditulis dalam tanda kutip ganda atau tunggal:
"John Doe" 'John Doe'
2. JavaScript Variables
Dalam bahasa pemrograman, variabel digunakan untuk menyimpan nilai data. JavaScript menggunakan kata kunci var untuk mendeklarasikan variabel. Tanda yang sama juga digunakan untuk menetapkan nilai ke variabel.
Dalam contoh ini, x didefinisikan sebagai variabel. Kemudian, x diberikan (diberi) nilai 6 :
var x; x = 6;
Javascript Comments (Komentar pada Javascript)
Komentar JavaScript dapat digunakan untuk menjelaskan kode JavaScript, dan membuatnya lebih mudah dibaca. Komentar JavaScript juga dapat digunakan untuk mencegah eksekusi, saat menguji kode alternatif.
1. Single Line Comments
Komentar satu baris dimulai dengan //. Teks apa pun antara // dan akhir baris akan diabaikan oleh JavaScript (tidak akan dieksekusi).
Contoh ini menggunakan komentar satu baris sebelum setiap baris kode :
// Ganti heading: document.getElementById("myH").innerHTML = "Halaman Pertamo"; // Ganti paragraf: document.getElementById("myP").innerHTML = "Halaman pertamo saya.";
Contoh ini menggunakan komentar satu baris di akhir setiap baris untuk menjelaskan kode :
var x = 5; // Deklarasi x, memberikannya nilai 5 var y = x + 2; // Deklarasi y, memberikannya nilai x + 2
2. Multi-line Comments
Komentar multi-baris dimulai dengan /* dan diakhiri dengan */.
Teks apa pun antara /* dan */ akan diabaikan oleh JavaScript.
Contoh ini menggunakan komentar multi-baris (blok komentar) untuk menjelaskan kode:
/* Contoh kode ini dan itu Menampilkan data komentar hanya terlihat di dalam kodingan */ document.getElementById("myH").innerHTML = "Halaman saya"; document.getElementById("myP").innerHTML = "Halaman kita semua.";
3. Menggunakan Komentar untuk Mencegah Eksekusi
Menggunakan komentar untuk mencegah eksekusi kode cocok untuk pengujian kode. Menambahkan // di depan baris kode mengubah baris kode dari baris yang dapat dieksekusi ke komentar.
Contoh ini menggunakan // untuk mencegah eksekusi salah satu baris kode :
//document.getElementById("myH").innerHTML = "Halaman Gendeng"; document.getElementById("myP").innerHTML = "Halaman Waw mantapu.";
Contoh ini menggunakan blok komentar untuk mencegah eksekusi beberapa baris :
/* document.getElementById("myH").innerHTML = "Halaman gendeng"; document.getElementById("myP").innerHTML = "Halaman Waw mantapu."; */
Semoga artikel ini bermanfaat untuk memahami dasar-dasar yang wajib anda ketahui sebelum melanjutkan pembelajaran javascript lebih lengkap. Silahkan beri komentar anda jika terdapat hal-hal yang belum anda mengerti.
I have read so many content about the blogger lovers except this
piece of writing is actually a fastidious paragraph, keep it up.
I think this is one of the most vital info for me. And
i am glad reading your article. But wanna remark on few general things, The website style is wonderful, the articles is really nice : D.
Good job, cheers
magnificent points altogether, you simply won a new reader.
What could you suggest in regards to your publish that you made some
days in the past? Any sure?
Valuable information. Lucky me I found your web site accidentally, and I am shocked why this twist of
fate did not came about earlier! I bookmarked it.
Very great post. I just stumbled upon your blog and wished to say that I’ve truly
enjoyed surfing around your weblog posts. After all I’ll be subscribing in your feed
and I’m hoping you write again soon!
Awesome article.
I really like it whenever people come together and share views.
Great website, stick with it!
Hi, i read your blog occasionally and i own a similar one
and i was just curious if you get a lot of spam responses?
If so how do you protect against it, any plugin or anything you can advise?
I get so much lately it’s driving me insane so any help is very much appreciated.
This piece of writing will assist the internet people for creating new blog or even a weblog
from start to end.
Hi, all is going well here and ofcourse every one is sharing facts,
that’s in fact good, keep up writing.
excellent submit, very informative. I wonder why the opposite specialists of this sector
do not understand this. You should proceed your writing.
I am confident, you have a great readers’ base already!
My programmer is trying to convince me to move to .net from PHP.
I have always disliked the idea because of the expenses.
But he’s tryiong none the less. I’ve been using
Movable-type on several websites for about a year and am anxious about
switching to another platform. I have heard very good things about blogengine.net.
Is there a way I can transfer all my wordpress content
into it? Any kind of help would be really appreciated!
Good way of describing, and fastidious article to take information concerning my presentation subject matter, which i am going to deliver
in academy.
I like it when individuals get together and share ideas.
Great website, continue the good work!
It’s awesome in support of me to have a web page, which is
good in favor of my knowledge. thanks admin
Hi, its pleasant piece of writing about media print, we
all know media is a fantastic source of data.
Wow, superb blog layout! How long have you been blogging for?
you made blogging look easy. The overall look of your web site is excellent, let
alone the content!
If you are going for most excellent contents like I do, simply pay a visit this web page all
the time since it provides feature contents, thanks
This is a topic that’s close to my heart… Many thanks!
Exactly where are your contact details though?
Heya just wanted to give you a brief heads up and let you know a few of the pictures
aren’t loading correctly. I’m not sure why but
I think its a linking issue. I’ve tried it
in two different browsers and both show the same results.
What’s Happening i am new to this, I stumbled upon this I have discovered It absolutely useful and it
has helped me out loads. I hope to contribute & assist other users like its
helped me. Great job.
I have been browsing online more than three hours today, yet I never found any interesting article like yours.
It’s pretty worth enough for me. Personally, if all web owners and bloggers made good content as you
did, the internet will be much more useful than ever before.
This post offers clear idea in favor of the new users of blogging, that in fact
how to do blogging and site-building.
Good day! I know this is kinda off topic but I was wondering
which blog platform are you using for this website?
I’m getting sick and tired of WordPress because
I’ve had issues with hackers and I’m looking at options for
another platform. I would be great if you could point me in the direction of
a good platform.
Superb site you have here but I was wanting to know if
you knew of any user discussion forums that cover the same topics
talked about in this article? I’d really love
to be a part of online community where I can get feedback from other knowledgeable people that share the same
interest. If you have any suggestions, please let me know.
Thanks!
Hi there, You have done an excellent job. I will
definitely digg it and personally suggest to my friends.
I am confident they’ll be benefited from this website.
What’s up, its fastidious article on the topic
of media print, we all understand media is a impressive
source of facts.
This paragraph provides clear idea designed for the new visitors of blogging,
that truly how to do blogging.
I was able to find good advice from your content.
Hello my family member! I want to say that this
article is amazing, nice written and come with almost all important infos.
I would like to look extra posts like this .
Hi there, just became alert to your blog through Google, and found that it’s
really informative. I’m gonna watch out for brussels.
I’ll appreciate if you continue this in future.
Many people will be benefited from your writing. Cheers!
I every time emailed this blog post page to all my associates, because
if like to read it next my contacts will too.
Hello! I simply would like to offer you a big thumbs up for the
excellent information you have got here on this post.
I’ll be coming back to your web site for more soon.
It’s actually very difficult in this busy life to listen news on Television, thus I
only use internet for that purpose, and take the latest news.
Its like you read my mind! You appear to know a lot about this, like you wrote the book in it or something.
I think that you could do with a few pics to drive the message home a
little bit, but instead of that, this is wonderful blog.
A fantastic read. I will definitely be back.
Wow, this piece of writing is pleasant, my younger sister is analyzing these things,
therefore I am going to convey her.
Wood household furniture has one thing really natural about it.
There is this feeling of coziness, of attributes and also of elegance that
may be be located in hardwood household furniture.
Hardwood is actually born coming from the earth.
It feeds the fire, breaks down in to ashes and also blows away.
It is quite close to the human life in the world. Might be
that is actually why it sounds so much with us. Might be actually that is actually why you still obtain that hot feeling when you
touch a rich mahogany desk.
I couldn’t resist commenting. Very well written!
Oh my goodness! Incredible article dude! Thanks, However I
am going through problems with your RSS. I don’t know the reason why I am unable to subscribe to it.
Is there anybody else having the same RSS problems?
Anyone who knows the solution will you kindly respond? Thanks!!
You really make it seem really easy together with your presentation but I find
this topic to be actually something that
I think I would never understand. It seems too complex and very broad for me.
I’m having a look ahead for your subsequent put up,
I will attempt to get the dangle of it!
Also visit my website :: attorney hail damage
Hello Dear, are you truly visiting this site daily, if so after that
you will without doubt obtain pleasant knowledge.
Very great post. I simply stumbled upon your weblog and wanted
to say that I have truly loved surfing around your blog
posts. After all I’ll be subscribing to your feed and I hope you write again very soon!
Excellent blog here! Also your website loads
up fast! What web host are you using? Can I get your affiliate link to your host?
I wish my website loaded up as fast as yours lol
Greetings! Very useful advice in this particular
post! It is the little changes that make the largest changes.
Thanks for sharing!
For newest news you have to visit internet and on internet I found this web page as a best website for hottest
updates.
Amazing blog! Is your theme custom made or did you download it
from somewhere? A theme like yours with a few
simple tweeks would really make my blog shine. Please let me know
where you got your design. Kudos
Hello there! Do you use Twitter? I’d like to follow you if that would be okay.
I’m definitely enjoying your blog and look forward to new posts.
bookmarked!!, I like your blog!
I think this is one of the so much significant info
for me. And i am satisfied reading your article. However want to statement on few common issues,
The web site taste is ideal, the articles is actually
nice : D. Just right activity, cheers
Having read this I believed it was very informative.
I appreciate you finding the time and energy to put
this informative article together. I once again find myself spending a significant amount of
time both reading and leaving comments. But so what, it was still worthwhile!