Lesson 1 of 28

In progress

Course progress

JavaScript

What JavaScript actually does

See where JavaScript fits beside HTML and CSS, run your first script in a page, and learn why defer matters.

HTML describes the page and CSS styles it. Neither can respond to a click, fetch new data or update a total as someone types. JavaScript is the layer that changes things after the page has loaded.

What you will learn

  • Place a script in a page and run it.
  • Explain why defer matters for scripts that touch the DOM.
  • Tell what JavaScript can and cannot do in a browser.

Three layers, three jobs

A useful way to hold the three languages apart:

  • HTML is the structure and the content.
  • CSS is the presentation.
  • JavaScript is the behaviour.

You can build a readable page with no JavaScript at all, and you should. A navigation menu that only works with JavaScript is a menu that disappears when a script fails. Add behaviour on top of working structure, not instead of it.

Your first script

HTML
Editable. Runs in your browser.





Press Run to see the output.

The script runs when the browser reaches it. Because it sits after the elements, getElementById finds them. Move the same script into the head and the elements do not exist yet, so the calls return null and the next property access throws.

That is the single most common first error in browser JavaScript, and it has two standard fixes.

defer, and why it is the default choice

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

defer downloads the file while the HTML is still being parsed and runs it after parsing finishes, in document order. The script can query any element without a DOMContentLoaded listener.

Without defer, a script in the head blocks parsing and runs too early. Without async, ordering is preserved, which matters when one file depends on another. Use defer for application scripts, and async only for independent things such as analytics.

What the browser gives you

JavaScript in a page can:

  • Read and change the DOM.
  • Respond to clicks, typing, scrolling and timers.
  • Make network requests with fetch.
  • Store small amounts of data in localStorage.
  • Use the device camera or location, with permission.

It cannot read arbitrary files from the user’s disk, start another program, or bypass the same-origin rules that stop one site reading another’s data. Those limits are deliberate, and they are why the web is safe to browse.

The same language also runs on servers with Node.js. This track stays in the browser, because every example can then run on this page without installing anything.

Where the output goes

console.log writes to the developer tools console, not to the page. Open the console with F12 or Ctrl+Shift+J. The page’s visitors never see it; it is a workspace for you.

To show something to a person, update the DOM: set textContent, toggle a class, or insert an element. That distinction — debugging output versus user-facing output — prevents a lot of confusion later.

JavaScript
Editable. Runs in your browser.





Press Run to see the output.

The first line is for you. The last three create real content in the page.

Strict mode and modules

Modern JavaScript files are strict by default when loaded as modules (type="module"). Strict mode turns silent mistakes into errors, such as assigning to an undeclared variable. Classic scripts are not strict unless you add "use strict"; at the top.

For now, write const and let, avoid undeclared assignments, and the difference will rarely bite. Module syntax appears later in the track.

Putting a script in the head that calls document.querySelector without defer runs before the body exists. The call returns null, and reading .textContent throws TypeError: Cannot read properties of null. Add defer or move the script to the end of the body.

Write a page with an empty h1 and a paragraph, then a script at the end of the body that fills both with text and logs a message to the console. Then move the script into the head without defer and describe what breaks.

Show solution
HTML
Editable. Runs in your browser.





Press Run to see the output.

Moved into the head without defer, both getElementById calls return null, so setting textContent throws and the heading stays empty. The console shows a TypeError pointing at the first failing line.

Recap

  • HTML is structure, CSS is presentation, JavaScript is behaviour.
  • Scripts run when parsed; defer waits for the document while keeping order.
  • Browser JavaScript can change the page and fetch data but cannot touch the file system freely.

Next, you will store values in variables and choose between const and let.

Do I need Node.js to learn JavaScript?
No. Everything in this track runs in a browser. Node becomes useful when you write build scripts, tests or servers, which is the last module.
Can I put JavaScript directly in HTML?
You can, with a script element. Separate files are easier to cache, test and reuse, so keep inline scripts for small page-specific setup only.

Progress is saved in this browser only. No account, no email.

Admin

About the author

Admin

Instructor and technical writer at tutorials.in

Writes beginner lessons for tutorials.in. Each lesson is checked against the language or tool documentation, and examples are run before publishing.

Reviewed by the editorial team before publishing. Last updated November 16, 2026.