๐Ÿ€„๐Ÿ€„
Defer
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