Defer is an attribute used in <script> tag when a external script is present using โsrcโ attribute. Defer firstly downloads the file and parse it parallelly. Using Defer we can save from null error!
Scenario where Defer is useful :
suppose we have on click event in our script file and after clicking the event we get a console log of โclickedโ
const btn = document.getElementById('button');
btn.addEventListener('click', () => {
console.log('clicked');
})
JavaScript
If we didnโt use defer into this our script wonโt work and will give null error because our HTML elements are still not loaded and script file is running which means practically there isnโt any button element hence we are getting null error!
Syntax to use Defer :
<script src="script.js" defer></script>
JavaScript
