Lesson 3: Functions and Events

Functions

Functions are blocks of code that perform tasks.

Example:

function greet(name) {
  console.log('Hello, ' + name + '!');
}
greet('Alice'); // Outputs: Hello, Alice!

Events

JavaScript reacts to user actions through events.

Example: Click Event

<button id="myButton">Click Me</button>
<script>
  document.getElementById('myButton').addEventListener('click', function() {
    alert('Button clicked!');
  });
</script>