KodingKaro
JavaScript · Beginner · Introduction to JavaScript

Script tags, engines, and the developer console

A browser engine reads JavaScript and executes it. Learn where scripts belong, how to defer them, and how the developer console helps find mistakes.

✓ What you will learn in this topic

  • Compare inline, internal, and external scripts.
  • Explain the purpose of defer.
  • Use console.log and inspect browser errors.

Concept explanation

A JavaScript engine parses and executes JavaScript; browsers embed engines such as V8 or JavaScriptCore. A script may be written in an HTML attribute, a script element, or a separate .js file. External files are easier to reuse and maintain. defer lets an external classic script wait until HTML parsing is complete before running.

A predictable script loading point prevents code from querying elements before they exist and keeps HTML focused on structure.

Syntax

Use in the document head, or place a script element near the end of body. Avoid inline event attributes such as onclick.

Example 1: a small experiment

Enter these statements in the browser developer console or save them in a .js file and run the file with Node.js.

console.log('Script loaded');
console.log(2 + 3);

Expected output: The console shows “Script loaded” and then 5.

Why it is useful: The console is useful for quick experiments and tracing values while debugging.

Example 2: a practical feature

Save this markup as index.html and create app.js beside it. In app.js, select #status and change its text. The defer attribute makes the parsed heading available first.

<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script src="app.js" defer></script>
  </head>
  <body>
    <h1 id="status">Ready</h1>
  </body>
</html>

Expected output: The page displays the heading; the deferred external script can safely find it after parsing.

Why it is useful: External files can be cached, reviewed, and organized independently of the page markup.

Common mistakes

JavaScript is not Java, and it does not replace HTML or CSS. Browser JavaScript has web-page APIs such as document; Node.js has server-side APIs and no document by default.

Best practices

Use an external script with defer for page behavior, and use the browser console to inspect errors and experiment without changing the source file.

Practice exercise

Create index.html and app.js. Load the external script with defer, update a heading from the script, and use the browser console to inspect one value. Then remove defer and explain why script placement matters.

Lesson summary

Browsers execute JavaScript with an engine, while developer tools expose a console for experiments and diagnostics.

Knowledge check

What does defer do for an external classic script?

Show explanation

A deferred script downloads while parsing continues and runs after parsing, before DOMContentLoaded.

Practical example

javascript
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <script src="app.js" defer></script>
  </head>
  <body>
    <h1 id="status">Ready</h1>
  </body>
</html>

Expected result:

The page displays the heading; the deferred external script can safely find it after parsing.

Hands-on practice task

Create index.html and app.js. Load the external script with defer, update a heading from the script, and use the browser console to inspect one value. Then remove defer and explain why script placement matters.

In this section

Other lessons in Introduction to JavaScript

Broaden your knowledge

Related learning topics