#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
JavaScript Fundamentals

Script element

The <script> element is how JavaScript gets into a web page. It either contains code directly or points to an external file with the src attribute. Where you place it and which attributes you add, especially defer, async and type="module", control when the code downloads and runs, which affects both page speed and whether your code can find the elements it needs.

Inline and external scripts

<!-- Inline: code written inside the element -->
<script>
  console.log('Hello from an inline script');
</script>

<!-- External: code loaded from a file -->
<script src="js/app.js" defer></script>

External files are usually better. The browser can cache them, several pages can share one file, and HTML and JavaScript stay separate. A script tag with a src must stay empty; code written between its tags is ignored.

How a plain script blocks the page

When the browser reads HTML from top to bottom and meets a normal <script src>, it stops building the page, downloads the file, runs it, and only then continues. A large script in the <head> therefore delays everything visible. Worse, the code runs before the body exists, so document.querySelector('#menu') returns null.

The traditional fix was to put scripts just before </body>. That works, but the download only starts once the browser reaches the bottom.

defer, async and module scripts

AttributeDownloadsRunsOrder kept?
noneImmediately, blocking parsingRight awayYes
deferIn parallel with parsingAfter the HTML is parsed, before DOMContentLoadedYes
asyncIn parallel with parsingAs soon as it arrivesNo
type="module"In parallelLike defer by defaultYes

For most of your own code, defer in the <head> is the best default: the download starts early, nothing blocks and the DOM is ready when the code runs. async suits independent scripts like analytics that do not depend on the page or on other scripts.

Module scripts

<script type="module" src="js/main.js"></script>

// js/main.js
import { formatPrice } from './utils.js';
document.querySelector('#price').textContent = formatPrice(499);

Modules can use import and export, run in strict mode automatically, keep their variables private instead of creating globals and are deferred by default. Note that the import path needs the file extension in the browser. Module scripts will not load from a page opened with file://; use a local dev server.

Mistakes with script tags

Using async for scripts that depend on each other is a frequent problem. If app.js uses a library loaded with async, whichever finishes downloading first runs first, so the app sometimes works and sometimes throws "is not defined". Use defer on both to keep order.

Another is writing a self-closing tag, <script src="app.js" />. HTML does not treat it as closed, so the rest of the page can be swallowed as script content. Always write a full </script> closing tag.

A third is a typo in the path. The page shows no error on screen, but the browser console and Network tab report a 404 for the file.

Choosing the right setup

  • Prefer external files over inline code.
  • Use defer for page scripts and async only for independent ones.
  • Use type="module" when you want import and private scope.
  • Always close the tag with </script>.

Ready to master real-world web development?

Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.

Explore Course