Skip to content
Justice Tech School

Where to Put JavaScript

JavaScript lives in a script element, either written between the tags or loaded from a .js file with the src attribute. External files are best because the browser can cache them and many pages can share one file.

Put scripts at the end of the body, or add the defer attribute in the head, so the page can display before the script runs.

Inline script

Example
console.log("This is an inline script running");

External file in a page

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

Exercise

Load the file after the page is parsed.

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

Quiz Test

Why is an external .js file usually better?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.