Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How should I use JavaScript in a web page?

JavaScript
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedSep 29, 2024
UpdatedNov 16, 2026
6 min read

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.

Table of Contents

  • Adding JavaScript to a web page
  • Changing the page with the DOM
  • Handling forms and user input
  • Building interactive elements
  • Loading data with fetch
  • Best practices for using JavaScript
  • Frequently Asked Questions
    • Where should I put the script tag?
    • Do I need a framework to use JavaScript?
    • What is the difference between inline and external JavaScript?
    • Is innerHTML safe to use?
  • Summary

Adding JavaScript to a web page

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.

html

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.

Changing the page with the DOM

The Document Object Model (DOM) is the live tree of elements the browser builds from your HTML. JavaScript can read and change it.

html

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.

Handling forms and user input

Start with built-in HTML validation (required, type="email", minlength) and add JavaScript for custom rules and messages.

html

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.

Building interactive elements

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.

html

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.

Loading data with fetch

Use fetch with async and await to get data from an API without reloading the page.

javascript
javascript
Loading syntax highlighting...

fetch only rejects on network failure, so check response.ok yourself. Top-level await works in module scripts.

Best practices for using JavaScript

  • Use const by default, let when a value changes, and avoid var.
  • Compare with === rather than == to avoid type coercion surprises.
  • Keep scripts in separate modules and split large files by feature.
  • Build with progressive enhancement: the page should still show its content and basic forms without scripts where possible.
  • Do not use document.write(); it is discouraged and can block parsing.
  • Never trust data from the browser; validate and escape on the server.
  • Use the browser developer tools console and debugger to find errors.

Frequently Asked Questions

Where should I put the script tag?

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.

Do I need a framework to use JavaScript?

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.

What is the difference between inline and external JavaScript?

Inline JavaScript sits in the HTML file, external JavaScript is loaded from its own file. External files are easier to maintain, cache and secure.

Is innerHTML safe to use?

Only with trusted content. For text coming from users, use textContent or sanitise the HTML first.

Summary

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.

Share