Performance glossaryDeferring JavaScript means using the defer attribute so a script downloads in the background and runs only after the HTML is parsed. It no longer blocks the page.
Deferring JavaScript means adding the defer attribute to a script tag:
<script src="/js/menu.js" defer></script>
Without it, the browser stops reading the HTML whenever it meets a script, downloads and runs the script, and only then continues. With defer, the script downloads in the background while the HTML is parsed, and runs once parsing is complete, just before the DOMContentLoaded event. The visible page can be built and painted without waiting for it.
Defer vs async
| Normal script | async | defer | |
|---|---|---|---|
| Blocks HTML parsing | Yes, while it downloads and runs | Only while it runs | No |
| When it runs | Immediately | As soon as it downloads | After the HTML is parsed |
| Keeps script order | Yes | No | Yes |
Because deferred scripts keep their order, defer is the safer choice for scripts that depend on each other, such as jQuery and the code built on it. async suits independent scripts such as analytics. There is more on the difference under asynchronous loading.
Two details catch people out: defer only works on external scripts with a src, not on inline scripts, and JavaScript modules (type="module") are deferred automatically.
Why it matters
Scripts in the <head> without defer or async are render-blocking: nothing appears until they have downloaded and run. PageSpeed Insights flags them as render-blocking resources; older tools called the fix "defer parsing of JavaScript". Deferring them usually improves First Contentful Paint and Largest Contentful Paint.
Deferring does not reduce how much JavaScript the page runs, so it does less for Interaction to Next Paint. For that, the scripts themselves have to get smaller or fewer.
How to defer JavaScript in WordPress
In a theme or plugin
Load scripts with wp_enqueue_script() and pass a loading strategy:
wp_enqueue_script( 'my-menu', get_theme_file_uri( 'js/menu.js' ), array(), '1.0', array( 'strategy' => 'defer', 'in_footer' => true ) );
WordPress adds the attribute but checks the dependency tree first: if a script that relies on this one would still load normally, WordPress keeps this one blocking too, so nothing runs out of order.
With a plugin
Performance plugins such as WP Rocket, Autoptimize, and LiteSpeed Cache can defer scripts site-wide, with options to exclude specific files. Some also offer to delay JavaScript until the visitor first interacts with the page, which is a different and more aggressive technique.
Common mistakes
- Deferring jQuery while inline scripts in the page still call it immediately. The result is "jQuery is not defined" errors in the console and broken sliders, menus, or forms. Exclude jQuery, or make sure the inline code runs after it.
- Deferring scripts that must run before the page appears, such as some consent or A/B testing tools, which can cause a flash of the wrong content.
- Not testing. After any change, check key templates, forms, and checkout with the browser console open.
Source: MDN: the script element.