Skip to content

Chapter 8 of 8

Browser Interaction and Best Practices

JavaScript's classic role is to manipulate the Document Object Model (DOM). You can select a single element with document.getElementById('id') or document.querySelector('.class'), and you can select many with document.querySelectorAll(), which returns a NodeList. Once you have an element, you can attach an event listener such as element.addEventListener('click', handler) and later remove it with removeEventListener(). The handler receives an event object e with details like e.target and helpers such as e.preventDefault() to cancel the browser's default behavior.

Instead of attaching listeners to many child elements, event delegation attaches a single listener to a parent and uses the fact that events bubble up through the DOM tree. This pattern improves performance, especially with hundreds of nodes, and works well for dynamically added elements. A common mistake is to add a separate listener to each child when one parent listener would handle them all. As with other techniques, trying it on a small realistic example and articulating the decision out loud—"what problem is this solving?", "what trade-off does it create?", and "how will I know it worked?"—is the best way to internalize the pattern before applying it broadly.

Modern JavaScript includes several conveniences and patterns worth knowing. Optional chaining (?.) safely reads nested properties or calls methods without throwing when an intermediate value is null or undefined. The nullish coalescing operator (??) returns its right-hand side only when the left side is null or undefined, unlike ||, which also treats values like 0, '', and false as falsy fallbacks. A pure function always returns the same output for the same input and avoids side effects such as mutating external state or making network calls, which makes programs easier to test and reason about. Debouncing delays a function call until activity stops, commonly used so that a search input triggers an API call only after the user pauses typing. Throttling ensures a function runs at most once per defined interval, ideal for handlers such as scroll, resize, and mousemove. Finally, embracing immutability—creating new arrays and objects rather than mutating existing ones—makes state changes predictable in UI frameworks and reducers; the common mistake is to mutate arrays or objects directly and expect change detection to work anyway. Together, these tools compose into idiomatic, robust JavaScript.

All chapters
  1. 1JavaScript Foundations
  2. 2Variables, Types, and Operators
  3. 3Control Flow and Functions
  4. 4Data Structures and ES6 Syntax
  5. 5Scope, Hoisting, and Closures
  6. 6Objects, Classes, and Built-in APIs
  7. 7Asynchronous JavaScript
  8. 8Browser Interaction and Best Practices

Drill it

Reading is not remembering. These come from the Javascript Fundamentals deck:

Q

What is JavaScript?

JavaScript is a high-level, interpreted programming language primarily used for web development to make web pages interactive. It is the scripting language of t...

Q

Who created JavaScript and in what year?

JavaScript was created by Brendan Eich in 1995 while working at Netscape. It was initially called Mocha, then LiveScript, before being renamed JavaScript.

Q

What is ECMAScript?

ECMAScript (ES) is the standardized scripting language specification upon which JavaScript is based. The latest stable version is ES2023, with JavaScript implem...

Q

How do you include JavaScript in an HTML file?

Use the <script> tag in the HTML, either inline with code between tags or externally via <script src="script.js"></script>. Place it before th...