Lesson 2 / 38

Adding JS to HTML

Script tag placement, and defer vs async.

The <script> tag

Link an external file, or write JS inline between the tags.

<script src="app.js"></script>

<script>
  console.log("Inline JS");
</script>

Where to place it

Put <script> just before </body>, or use defer in <head> — both let the HTML load first so the page isn't blocked.

defer vs async

defer runs scripts in order after parsing finishes; async runs as soon as it's downloaded — order isn't guaranteed.

<script src="a.js" defer></script>
<script src="b.js" async></script>