\n\n\n\nKey points:\n\nA

LOGBOOK

HELP

Quiz Entry - updated: 2026.07.05

How do you embed JavaScript inside an HTML document?

You put JavaScript inside a <script> element, which the browser runs the moment it reaches that element.

The <script> tag is the bridge between HTML and JavaScript. The browser reads an HTML page from top to bottom, and when it hits a <script> element it stops and runs the code inside before continuing:

<html>
<head>
    <title>Hello World</title>
</head>
<body>
    <script>
        console.log('Hello JavaScript');
    </script>
</body>
</html>

Key points:

  • A <script> can live in the <head> or the <body> — both work.
  • The code runs as soon as the browser processes that element, in document order.

This ordering creates a classic gotcha: if a script in the <head> tries to touch a button that appears later in the <body>, the button doesn't exist yet and the script fails. The common fix is to put scripts at the end of the <body>, or add the defer attribute (<script defer src="...">), which tells the browser to wait until the HTML has finished loading before running the script — also avoiding a pause in page rendering.

Go deeper:

  • doc MDN: <script> element — full reference with diagrams comparing plain, defer, and async loading and execution order.

From Quiz: WEBT / Introduction to JavaScript | Updated: Jul 05, 2026