Ana kuruluşumuz:
JavaScript; web sayfalarına etkileşim, hareket, hesaplama, veri işleme ve oyun mekaniği eklemek için kullanılan bir programlama dilidir.
HTML → yapı
CSS → tasarım
JavaScript → hareket / mantık
Modern ve en çok tercih edilen değişken tanımlama.
let age = 20;
console.log(age);
✔️ Değiştirilebilir değerler için kullanılır.
Değişmeyen değerler.
const pi = 3.14;
console.log(pi);
Artık önerilmez.
let name = "Ahmet"; // string
let age = 15; // number
let isOpen = true; // boolean
let user = { name: "Ali", age: 12 }; // object
let colors = ["red","blue"]; // array
let empty = null;
let noDefined; // undefined
5 + 3
5 - 3
5 * 3
5 / 3
5 % 2 // mod
5 > 3
5 < 3
5 === 5
5 !== 3
let score = 75;
if (score >= 50) {
console.log("Geçtin!");
} else {
console.log("Kaldın!");
}
for (let i = 0; i < 5; i++) {
console.log(i);
}
let x = 0;
while (x < 3) {
console.log(x);
x++;
}
function hello() {
console.log("Merhaba dünya!");
}
hello();
const topla = (a, b) => a + b;
console.log(topla(3, 5));
let user = {
name: "Gülsün",
age: 30,
speak() {
console.log("Merhaba!");
}
};
console.log(user.name);
user.speak();
let fruits = ["elma", "armut", "muz"];
console.log(fruits[0]); // elma
fruits.push("çilek"); // sona ekle
console.log(fruits.length); // uzunluk
Web sayfasındaki öğeleri değiştirmek için kullanılır.
<p id="text">Hello</p>
<button onclick="change()">Değiştir</button>
<script>
function change() {
document.getElementById("text").innerText = "Merhaba JS!";
}
</script>
<button id="btn">Tıkla</button>
<script>
document.getElementById("btn").onclick = () => {
alert("Tıklandı!");
};
</script>
let data = {
name: "Ali",
age: 10
};
console.log(JSON.stringify(data)); // objeyi JSON yapar
fetch("https://api.example.com/users")
.then(res => res.json())
.then(data => console.log(data));
try {
let x = y + 3; // hata
} catch (err) {
console.log("Bir hata oluştu!", err);
}
class Person {
constructor(name) {
this.name = name;
}
speak() {
console.log("Ben " + this.name);
}
}
let p = new Person("Kerem");
p.speak();