Loops and Events
Loops and events are essential concepts in web programming that make websites dynamic and interactive.
Here are some simple explanations and examples of each:
Code sample 1: using a loop
const hobbies = ["Fishing", "Gaming", "Cooking"];
for (let i = 0; i < hobbies.length; i++) {
console.log("I enjoy " + hobbies[i] + ".");
}
The loop iterates through the hobbies array and displays each hobby to the console log.
Code sample 2: using an event
document.getElementById("clickMe").addEventListener("click", function(){
alert("Button clicked!");
});
When the button is clicked, an event triggers a function that displays an alert box. Give it a try!
Both loops and events are powerful tools for creating interactive web applications. Understanding these concepts is a great step in making your websites fun!