Modul Pemrograman Web

Materi JavaScript

JavaScript adalah bahasa pemrograman yang digunakan untuk membuat halaman web interaktif. Berikut adalah beberapa konsep dasar dalam JavaScript:

Contoh Konsep Dasar JavaScript

1. Variabel

var x = 5;
let y = 10;
const z = 15;
console.log(`x : ${x}`)
console.log(`y : ${y}`)
console.log(`z : ${z}`)

2. Tipe Data

let name = "John";
let age = 30;
let isStudent = true;
console.log(`name : ${name}`)
console.log(`age : ${age}`)
console.log(`isStudentt : ${isStudent}`)

3. Fungsi

function greet(name) {
  return "Hello, " + name + "!";
}
let greeting = greet("World");

4. Percabangan

let x = 10
if (x > 0) {
  console.log("Positif");
} else {
  console.log("Negatif");
}

5. Perulangan

for (let i = 0; i < 5; i++) {
  console.log(i);
}

6. Objek

let person = {
  name: "John",
  age: 30,
  isStudent: true
};

7. Array

let numbers = [1, 2, 3, 4, 5];
let fruits = ["Apple", "Banana", "Orange"];

8. Event

document.getElementById("myButton").addEventListener("click", function() {
  console.log("Button clicked");
});

9. AJAX

let xhr = new XMLHttpRequest();
xhr.open("GET", "https://api.example.com/data", true);
xhr.send();
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    console.log(xhr.responseText);
  }
};