Member-only story
Defer vs Async in JavaScript: What’s the Best Way to Load Scripts?
When it comes to optimizing website performance, how you load JavaScript files can make a big difference. One of the most common questions developers ask is: “Defer vs Async — which should I use to load scripts?” In this guide, we’ll show you how both work, and help you choose the right one.
Why Script Loading Matters
JavaScript is powerful, but if it’s not loaded properly, it can slow down your site. When a browser encounters a <script>
tag, it has to decide whether to load it immediately or continue parsing the HTML.
Traditionally, scripts block the rendering of the page. That means users wait longer to see content. This is where defer
and async
come in.
Understanding the Basics
There are three common ways to load external scripts in HTML:
<script src="script.js"></script> <!-- Default behavior -->
<script src="script.js" defer></script> <!-- Defer -->
<script src="script.js" async></script> <!-- Async -->
Each one tells the browser something different about when and how to load the JavaScript.