Does it matter if we remove the defer from the script tag? By putting
the script at the end of the body tag is already delaying the
execution of the script so the above code should be the same as the
following snippet right?
Right you are! In this case, I'd say that there is no difference. As you correctly mention: the body tag is before the script, and that means it gets parsed first. Your html file has no idea there is any javascript until it has fully parsed the preceding code, and therefore defer does nothing.
Defer's only job is to run JavaScript after all the html has been parsed: see the MDN docs.
That said, with slightly different code, with say, another <script />
underneath is where the difference is found. That second (or more) <script />
tag will then begin to download in parallel (but executed in order). For example:
<script src="script1.js" />
<script src="script2.js" />
script1 is downloaded first, blocking script2 from downloading.
<script src="script1.js" defer />
<script src="script2.js" defer />
script1 is starts to download, then script2 also starts to download in parallel — but — script1 still gets executed in first, even if script2 finishes downloading first.
Further reading:
Hope this helps!