Lesson 3: The <script> Tag, External Scripts & Browser Execution with challenges!

๐ธ Whatโs the <script>
tag?
The <script>
tag tells the browser: โก๏ธ "Hey! Here's some JavaScript. Run it!"
You can put it:
Inline, directly inside HTML
Externally, loaded from a
.js
fileModify execution with
async
ordefer
โ Example 1: Inline Script
<html>
<body>
<p>Before script</p>
<script>
alert('Hello, world!');
</script>
<p>After script</p>
</body>
</html>
๐ This runs immediately, blocking HTML parsing.
โ Example 2: External Script
<script src="script.js"></script>
If script.js
contains:
console.log('Running external script');
๐ฅ Itโs cleaner, cacheable, and reusable.
โ Invalid: Mixing Inline + src
<script src="script.js">
console.log('Ignored!');
</script>
Only the external script runs โ inline code is ignored.
โ Example 3: Multiple External Scripts
<script src="lib.js"></script>
<script src="main.js"></script>
Runs in order, one after another โ unless async or defer is used.
โก Script Loading: async
vs defer
Attribute | Download Timing | Blocks HTML? | Execution Timing | Order Preserved? |
(default) | Now | โ Yes | Immediately after download | โ Yes |
async | Now | โ No | Immediately after download | โ No |
defer | Now | โ No | After HTML parsing complete | โ Yes |
๐ Visual Flow
HTML Parsing โ
โโโ <script>
โโโ Inline? โ Run now (block)
โโโ External?
โโโ async โ Download & run ASAP (random order)
โโโ defer โ Download, wait, then run in order
๐น 2. Fill Any Gaps
๐ณ๏ธ Hidden Quirks
Quirk | Detail |
type="text/javascript" | โ Not needed anymore (default is JS) |
language="javascript" | ๐ซ Obsolete legacy attribute |
Inline + src together | โ Only the src runs |
Blocking behavior | โณ Scripts pause HTML parsing unless using async or defer |
Inline ignores defer/async | โ Only external scripts support these attributes |
Browser caching | โ External scripts are cached and reused |
๐ Internal Mechanics
HTML is parsed top to bottom.
When a <script>
is hit:
Parser pauses
JavaScript is downloaded (if
src
)JS is executed
Parsing resumes
Unless async
or defer
are used.
๐น 3. Challenge Me Deeply
โ Beginner
Add an inline script that logs
"Script loaded!"
Create
log.js
and load it viasrc
Prove inline code inside
<script src="">
is ignored
๐งฉ Intermediate
Load two scripts (
a.js
,b.js
) withasync
โ observe unpredictable orderChange to
defer
โ observe guaranteed orderAdd a script in
<head>
withdefer
, log when it runs (after DOM loaded?)
๐ฅ Advanced
Dynamically inject a
<script src="...">
tag with JSUse
script.onload
to detect load completionWrite a fallback if CDN fails:
onerror
+ local scriptMeasure time difference between blocking vs defer script loading
Use
document.readyState === 'interactive'
check to testdefer
๐ง Brain Twister
Can you write a single HTML file that:
Loads external script
Logs something before and after it
Maintains correct execution order?
๐น 4. Interview-Ready
โ Concept-Based Questions
What is the difference between inline and external scripts in HTML?
Why does placing a
<script>
in the<head>
withoutdefer
cause page load delays?Explain the difference between
async
anddefer
. When would you use each?Can a
<script>
tag have bothsrc
and inline code? What happens?What does
defer
guarantee in terms of execution timing and order?Is JavaScript execution synchronous or asynchronous in the browser context?
How does the browser treat inline scripts differently from external ones?
๐ Scenario-Based Questions
You have two scripts:
vendor.js
andapp.js
. App relies on vendor. How do you ensure correct order without blocking the HTML parse?- Expected: Use
defer
on both; order in HTML determines execution.
- Expected: Use
Your external script loads too slowly and blocks the rendering. What are your options?
- Expected: Move script to end of
<body>
, usedefer
, or load it async if it's independent.
- Expected: Move script to end of
You're using
async
, but your dependent scripts break. Why?- Expected:
async
doesn't guarantee execution order โ switch todefer
.
- Expected:
A script is trying to access a DOM element, but
null
is returned. What might be the issue?- Expected: DOM not yet fully parsed. Either move the script after the element or use
defer
orDOMContentLoaded
.
- Expected: DOM not yet fully parsed. Either move the script after the element or use
You use inline JavaScript in
<head>
that accesses elements in<body>
, and it fails. How can you fix this?- Expected: Move script to the bottom of
<body>
, or wrap code inwindow.onload
orDOMContentLoaded
.
- Expected: Move script to the bottom of
๐ Debugging Questions
Why would a script that logs to the console not appear in DevTools at all?
- Expected: Maybe itโs in a
<script>
tag with bothsrc
and inline code โ inline is ignored.
- Expected: Maybe itโs in a
Your external script logs โLoadedโ but the DOM updates donโt happen. What could go wrong?
- Expected: Script may run before DOM is ready; need to defer or wait for DOM.
Script works locally but fails on production. What could be causing this?
- Expected: Caching issues, file not found (check
src
paths), CSP policies, or missingdefer
.
- Expected: Caching issues, file not found (check
You load 3 async scripts and get inconsistent behavior. Why?
- Expected: Execution order not guaranteed with
async
.
- Expected: Execution order not guaranteed with
๐ฉ Red Flags Interviewers Look For
Red Flag | Why Itโs Bad |
Using <script> in <head> without defer | Blocks rendering |
Mixing src and inline code | Only src runs, inline ignored |
Relying on alert() for debugging | Poor practice |
Using async for dependent scripts | Risk of race conditions |
Assuming scripts run after DOM automatically | Not true unless defer , async , or DOMContentLoaded used |
โ Best Practices to Mention
Always use
defer
for scripts that interact with DOM and donโt need early execution.Use
async
only for scripts that are truly independent (analytics, ads, etc.).Keep scripts modular and ordered properly.
Prefer external scripts for caching and separation of concerns.
Use
DOMContentLoaded
orwindow.onload
for DOM-dependent inline code.
๐น 5. Real-World Usage
Area | Use Case |
HTML Templates | Load modular scripts per page |
SPA Frameworks | Webpack/Vite output bundles via <script src=""> |
Analytics | Load Google Analytics / Meta Pixel (often async ) |
A/B Testing | Inject scripts based on user groups |
๐น 6. Remember Like a Pro
๐ง Mnemonic
"SRC means Script OR Code โ not both!"
Async = โAs soon as readyโ
Defer = โDelay until DOM doneโ
๐ Cheatsheet
<!-- โ
Inline script -->
<script>
console.log("Hello from inline script");
</script>
<!-- โ
External script -->
<script src="main.js"></script>
<!-- โ
External with defer -->
<script src="main.js" defer></script>
<!-- โ Don't do this -->
<script src="main.js">
console.log("This wonโt run");
</script>
๐น 7. Apply It in a Fun Way
๐ Mini Project: Script Execution Tester
Create a simple HTML page that:
Loads 3 script tags:
One inline
One external (
log.js
)One invalid (
src
+ inline code)
Logs order of execution
Dynamically creates a
<p>
tag from each script, color-coded
Bonus Features:
Toggle
async
vsdefer
Add artificial delay using
setTimeout
Show timeline bar of which script runs when
๐ง Extra Value
โ
Every website uses this โ itโs fundamental
โ ๏ธ Common Mistake: Putting scripts too early
๐ Performance Tip: Use defer
in <head>
๐ Modern Loaders: Use onload
/ onerror
for reliability
Subscribe to my newsletter
Read articles from manoj ymk directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
