Recommended: Yes ✅
<script src="script.js" defer></script>
Option A: At the bottom of <body>
(works, older style):
<body>
...
<script src="script.js"></script>
</body>
Option B (Better): Use defer
in the <head>
<head>
<script src="script.js" defer></script>
</head>
async
Unless NeededUse only for independent scripts like analytics.
Use sparingly: okay for very small tasks.
<script>
alert('Hello');
</script>
Use tools like Terser, Webpack, or Vite for smaller file sizes and faster load.
Use let
, const
, arrow functions, etc. if you're not supporting old browsers.
<script type="module" src="main.js"></script>
Supports import/export
and automatically defers execution.
Practice | Recommendation |
---|---|
External scripts | ✅ Yes |
defer in <head> |
✅ Yes |
Inline scripts | ❌ Avoid unless very small |
async |
⚠️ Only for independent scripts |
Minify in production | ✅ Yes |
Modern syntax/modules | ✅ Yes |