Einfache Beispiele
Variables
Variables store data values. In JavaScript you can declare them with let, const, or var.
Tip: Use const by default, and only use let when you need to reassign.
Functions
Functions are reusable blocks of code that perform specific tasks.
function greet(name) {
return `Hello, ${name}!`;
}
const result = greet("Alice");
Hello, Alice!
Mittlere Beispiele
DOM Manipulation
JavaScript can modify page content, styles, and structure.
This element would change with JavaScript
document.getElementById("demo-text").innerText = "New text content";
Event Handling
JavaScript can respond to user interactions like clicks, mouse movements, and key presses.
No interaction yet
button.addEventListener("click", () => {
statusElement.innerText = "Button clicked!";
});
Fortgeschrittene Beispiele
Fetch API
JavaScript can fetch data from APIs and update the page without reloading.
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => displayData(data));
Async/Await
Modern syntax for working with asynchronous operations in a more readable way.
async function fetchData() {
try {
const response = await fetch(url);
const data = await response.json();
return data;
} catch (error) {
console.error(error);
}
}