How to link Javascript to HTML

When linking JavaScript to HTML, does it matter where the < script>tag is placed? What’s the best practice?

Yes, it definitely matters where you place the <script> tag, because it affects page loading, performance, and errors.


:small_blue_diamond: Why script placement matters

HTML is loaded top to bottom.
JavaScript can block page rendering while it’s being downloaded and executed.

If JavaScript runs before the HTML elements exist, you may get errors like:

Cannot read property '...' of null


:small_blue_diamond: Common script placements

:one: Inside <head> (:cross_mark: not recommended by default)

<head>
  <script src="script.js"></script>
</head>

Problem:

  • Script loads before the DOM is ready

  • Can slow down page rendering

  • DOM elements may not exist yet

:check_mark: Works only if you use defer or DOM-ready logic


:two: Before closing </body> (:white_check_mark: best practice for beginners)

<body>
  <!-- HTML content -->

  <script src="script.js"></script>
</body>

Why this is good:

  • HTML loads first

  • JavaScript runs after DOM is ready

  • Avoids most DOM-related errors


:three: Using defer in <head> (:white_check_mark: modern best practice)

<head>
  <script src="script.js" defer></script>
</head>

What defer does:

  • Downloads script in parallel with HTML

  • Executes after HTML parsing is complete

  • Preserves script order

:backhand_index_pointing_right: This is the recommended approach in modern web development


:four: Using async (:warning: use carefully)

<script src="script.js" async></script>

Behavior:

  • Loads asynchronously

  • Executes immediately when ready

  • Can break order-dependent scripts

Best for:

  • Analytics

  • Independent scripts


:small_blue_diamond: Best practice summary

Method Recommended
Script at end of body :white_check_mark: Yes
<script defer> in head :white_check_mark: Best (modern)
Script in head without defer :cross_mark: Avoid
async for main logic :cross_mark: Usually no

:small_blue_diamond: Simple rule to remember

If your script touches the DOM → use defer or place it before </body>

1 Like

JavaScript can be linked to HTML in a few simple ways. The most common and recommended method is using the <script> tag.

1. External JavaScript file (best practice):

<script src="script.js"></script>

Place this tag just before the closing </body> tag so the HTML loads first.

2. Inside the HTML file (internal script):

<script>
  console.log("Hello World");
</script>

3. In the <head> section (with defer):

<script src="script.js" defer></script>

Using defer ensures JavaScript runs after the HTML is fully loaded.

Tip: For clean and maintainable code, use external JavaScript files and avoid inline scripts unless necessary.

1 Like

Yes, the placement of the <script> tag does matter.

The safest and most commonly used approach is to place the JavaScript file at the bottom of the <body>. This ensures that the HTML loads first, so your JavaScript can access all elements without errors.

Another modern best practice is to place the script in the <head> with the defer attribute. This allows the browser to load the JavaScript in parallel and execute it only after the HTML is fully parsed.

Placing a <script> in the <head> without defer is generally not recommended because it can block page loading and cause issues if the DOM isn’t ready.

Quick summary:

  • Bottom of <body> → simple and safe

  • <head> with defer → modern and efficient

  • <head> without defer → avoid

This is a small detail, but it makes a big difference in real-world web development.

1 Like