</>
Skip to content
JavaScript lessons (3/64)

JavaScript — Where To

Three homes for JavaScript

1. Between <script> tags

<script>
    function sayHello() {
        alert("Hello!");
    }
</script>

Inline code lives right inside the HTML page.

2. In <head> — with a caveat

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

Head scripts download and run before the page renders. A script that touches page elements here finds… nothing yet:

// app.js loaded from <head>
document.getElementById("demo");   // null — <p id="demo"> doesn't exist yet!

Classic first-week bug: "my script can't find my element."

Fixes, best first:

<!-- defer: download early, run AFTER document is parsed -->
<script src="app.js" defer></script>
head + defer = fastest loading AND elements exist.
This is the modern default for site-wide scripts.

3. Before </body> — the simple classic

<body>
    <h1 id="demo">Page</h1>

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

By the time this script runs, everything above it exists. Zero surprises; slightly slower start for big files than defer.

External files — the professional default

<script src="js/app.js"></script>
BenefitDetail
CachingDownloaded once per user, reused across pages
ReuseSame script serves ten pages
MaintainabilityJS code lives in JS files, not tangled in markup
CleanlinessNo quote-escaping gymnastics

Note: external scripts still need a closing tag: <script src="app.js"></script> — self-closing syntax silently fails.

Multiple scripts run in order:

<script src="utils.js" defer></script>
<script src="app.js" defer></script>   <!-- waits for utils.js -->

defer preserves order; async runs each the moment it arrives (order unpredictable) — use only for independent scripts like analytics.

Inline event handlers — know them, then avoid them

<button onclick="sayHello()">Hi</button>

Works everywhere and fills old tutorials — but mixes behavior into markup. The scalable pattern:

<button id="hello-btn">Hi</button>

<script>
    document.getElementById("hello-btn")
            .addEventListener("click", sayHello);
</script>

HTML stays declarative; JS owns behavior; one listener setup handles any button.

Placement decision table

SituationChoice
Site-wide app logic<head> + defer
Quick page experimentbottom of <body>
Third-party analyticsasync in head
Tiny demo/learninginline <script> block

Mini Practice

  1. Put a getElementById script in <head> — witness the null
  2. Fix it with defer; verify
  3. Move the script to end-of-body as an alternative fix
  4. Split code into two files; confirm execution order with logs
  5. Convert an onclick attribute to addEventListener

Next: output → (already written)

Related Topics

Frequently Asked Questions about Where To

What is Where To in JavaScript?

Where To is a fundamental concept in JavaScript. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Where To?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Where To.

Why is Where To important in JavaScript?

Where To is essential for JavaScript development. Understanding this concept will help you write better code and solve real-world problems more effectively.