Inline handler
Example: onclick on an element. Easy to start, hard to maintain.
JavaScript can be written inline, inside a page or in external files, but production projects usually load external scripts with a clear loading strategy.
<script type="module" src="/assets/js/app.js"></script>
// app.js
import { setupMenu } from "./menu.js";
setupMenu();
JavaScript Basics
Browsers read HTML from top to bottom. A script can block parsing, run before elements exist or run after the page is ready depending on how you include it.
For tiny demos, inline scripts are acceptable. For real websites, use external files. External files are easier to cache, review, reuse and organize.
Modern frontend code often uses type="module". Modules are deferred by default, can import other files and keep variables scoped to the module instead of leaking everything globally.
Example: onclick on an element. Easy to start, hard to maintain.
A script tag in the HTML page. Useful for demos or page-specific bootstrapping.
A separate .js file loaded by the page. Best default for real projects.
A modern external script that can import and export code cleanly.
Examples
<head>
<link rel="stylesheet" href="/assets/css/styles.css">
<script type="module" src="/assets/js/site.js"></script>
</head>
// site.js
import { setupNavigation } from "./navigation.js";
import { setupForms } from "./forms.js";
setupNavigation();
setupForms();
<head> <script src="/assets/js/site.js"></script> </head> <button onclick="openMenu()">Menu</button> <script> var globalMenuState = false; </script>
Rules that matter
JavaScript becomes much easier when you understand why a pattern exists. The syntax is only the surface; the real skill is choosing the right behavior for the interface.
They keep templates clean and make JavaScript easier to cache and maintain.
Modules defer automatically and give you import/export structure.
defer lets HTML parsing continue and runs the script after the document is parsed.
They mix structure and behavior and become hard to test.
A selector returns null if the element has not been parsed yet.
A main file should set up features, not contain every detail of the entire site.
Production thinking
Bad script placement causes slow pages, broken selectors and code that nobody wants to touch. Good placement makes the page faster and the project easier to reason about.
If JavaScript controls navigation, forms or modals, it must load reliably. A broken script can trap users or hide important controls.
Use external files, modules, bundling when needed and clear loading order. Avoid random inline snippets spread through templates.
Important links and content should not depend on late or fragile JavaScript. Crawlers handle JavaScript better than before, but reliable HTML is still stronger.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
Do not only read the topic. Change the code, explain what happened and answer the questions in your own words.