Use JavaScript by adding it to an HTML page with a <script type="module"> element (or an external .js file), selecting elements with document.querySelector, and reacting to user actions with addEventListener. Keep the code in separate files, prefer textContent over innerHTML, and validate everything again on the server.
The examples below cover the most common jobs: changing the page, handling forms, building interactive widgets and loading data, written with current browser APIs.
There are two ways to include a script. An inline script lives inside the HTML, while an external script is a separate file referenced with src.
A type="module" script is deferred automatically, so it runs after the HTML has been parsed and the elements already exist. For a classic script placed in the head, add the defer attribute for the same effect. External files are cached by the browser and work with a strict Content Security Policy, so they are the better default.
The Document Object Model (DOM) is the live tree of elements the browser builds from your HTML. JavaScript can read and change it.
Use addEventListener instead of inline onclick attributes, so behaviour stays out of the markup. Use textContent to insert plain text. Assigning user-supplied data to innerHTML can create cross-site scripting (XSS) vulnerabilities, so only use it with trusted, sanitised markup.
Start with built-in HTML validation (required, type="email", minlength) and add JavaScript for custom rules and messages.
The handler cancels the submission with event.preventDefault() when the name is empty, shows the message in an element with role="alert" so screen readers announce it, and moves focus to the field. Do not use alert() for errors and do not rely on the browser check alone, because the server must validate the same data.
Menus, tabs, modals and sliders follow the same pattern: keep state in a variable, update the DOM when it changes, and use real buttons so keyboard users can operate them.
The modulo operator wraps the index back to zero after the last image, and the alt text is updated with each change. For common widgets such as dialogs, check the native <dialog> element first, as it gives you focus handling and keyboard support without extra code.
Use fetch with async and await to get data from an API without reloading the page.
fetch only rejects on network failure, so check response.ok yourself. Top-level await works in module scripts.
const by default, let when a value changes, and avoid var.=== rather than == to avoid type coercion surprises.document.write(); it is discouraged and can block parsing.Put it in the head with type="module" or defer, or at the end of the body. Both make sure the elements exist when the script runs.
No. Plain JavaScript and the DOM are enough for many pages. Frameworks such as React or Vue help when an interface has a lot of shared state.
Inline JavaScript sits in the HTML file, external JavaScript is loaded from its own file. External files are easier to maintain, cache and secure.
innerHTML safe to use?Only with trusted content. For text coming from users, use textContent or sanitise the HTML first.
Include scripts as external modules, find elements with querySelector, respond with addEventListener, and fetch data with async and await. Prefer textContent, accessible markup and built-in HTML features, and always validate on the server as well.