5 Contoh Coding Javascript
JavaScript adalah bahasa pemrograman yang paling populer di dunia. Mengapa begitu ? karena JavaScript bisa digunakan di sisi front end ataupun back end. Untuk mempelajarinya kita harus menginstall Text Editor dan juga Browser.
1. Output dengan innerHTML
<!DOCTYPE html> <html> <body> <h2>My First Web Page</h2> <p>My First Paragraph.</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = 5 + 6; </script> </body> </html>
Hasilnya :
2. Variabel
<!DOCTYPE html> <html> <body> <h2>JavaScript Variables</h2> <p>In this example, x, y, and z are variables.</p> <p id="demo"></p> <script> var x = 5; var y = 6; var z = x + y; document.getElementById("demo").innerHTML = "The value of z is: " + z; </script> </body> </html>
Hasilnya :
3. Operator
<!DOCTYPE html> <html> <body> <h2>JavaScript Operators</h2> <p>x = 5, y = 2, calculate z = x + y, and display z:</p> <p id="demo"></p> <script> var x = 5; var y = 2; var z = x + y; document.getElementById("demo").innerHTML = z; </script> </body> </html>
Hasilnya :
4. Aritmatika
<!DOCTYPE html> <html> <body> <p>A typical arithmetic operation takes two numbers and produces a new number.</p> <p id="demo"></p> <script> var x = 100 + 50; document.getElementById("demo").innerHTML = x; </script> </body> </html>
Hasilnya :
5. Event
<!DOCTYPE html> <html> <body> <button onclick="document.getElementById('demo').innerHTML=Date()">The time is?</button> <p id="demo"></p> </body> </html>
Hasilnya :
Komentar
Posting Komentar