How can event listeners and avoiding inline JavaScript improve the code structure and functionality?

Using event listeners instead of inline JavaScript improves code structure by separating the behavior from the HTML markup, making the code more maintainable and easier to read. It also allows for better organization of event handling functions and promotes reusability. By avoiding inline JavaScript, it reduces the risk of mixing presentation logic with functionality, leading to a cleaner and more scalable codebase.

<!DOCTYPE html>
<html>
<head>
    <title>Event Listener Example</title>
</head>
<body>
    <button id="myButton">Click me</button>

    <script>
        document.getElementById('myButton').addEventListener('click', function() {
            alert('Button clicked!');
        });
    </script>
</body>
</html>