Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

addEventListener
A call on an element that registers a listener for one kind of event.
closest
A way to find the nearest parent of an element that matches a rule you give, starting with the element itself.
closure
A function together with the variables from the place it was written, which stay available when it runs later.
default behaviour
What the browser does on its own when an event happens, such as following a link or submitting a form, before any script intervenes.
DOM
The tree of objects a browser builds from the page, which a script can read and change while the page is open.
element
One item in the page tree, such as a heading or a button, which a script can find and change.
event
Something that happens on the page, such as a click, a key press, or a form being submitted, which a script can respond to.
event bubbling
The way an event rises from the element that received it up through each of its parents.
event target
The element that actually received the action, which may be a child of the element whose listener is running.
function
A reusable block of code that can be called with values and can return an answer to whoever called it.
handler
The function you give to a listener, which the browser runs when the event it waits for happens.
listener
A function registered on an element to wait for one kind of event and run each time that event reaches it.
property
A named value stored on an object, usually read and written with a dot, such as the text of an element.
reference
A value that points at an object rather than holding a copy, so a change made through one is visible through the other.
scope
The part of a program where a name can be used, decided by where it was declared.
variable
A name that holds a value so you can use it again; a constant cannot be pointed at a different value later.

JavaScript from the ground up

How JavaScript works

The third language of the web: what a script is for, how the page loads one, and where its output appears.

Part 1 of 12

Beginner12 min readBack to Courses

The third language

HTML says what things are, CSS says how they look, and JavaScript says what happens. It is the only one of the three that can decide — read what a visitor typed, react to a click, change the page after it has loaded.

A script is a file of instructions, and the page brings it in with a <script> tag. The browser reads it from the top, line by line, the moment it reaches that tag — the same way Python reads a program, except the page is already on screen while it runs.

It is also the only one of the three that can be switched off. A visitor with scripts disabled still gets the HTML and the CSS, so the page should make sense without the behaviour, and the behaviour should add to it rather than replace it. That habit is called progressive enhancement, and it starts with putting the words in the markup.

A script that says something

console.log("Hello from the page");

const name = "Ada";
console.log("Hello, " + name + "!");

Where the output goes

On this page, console.log prints to the console pane under the preview — the black strip that also shows errors. In a browser tab you would open the developer tools to see the same thing; here it is already open.

That matters more than it sounds. JavaScript has no window to print into the way Python does, so the console is how a program talks while you are building it — and how it tells you what went wrong when a line cannot run.

Errors point at a line

When a line cannot run, the console shows a message and a line number. A misspelled name gives "is not defined"; a missing bracket gives a syntax error before anything runs at all.

Both are normal, and both are information. The line number is where you look first — and a console message you do not understand is usually one search away from being one.

What the browser does with the file

The browser reads an HTML file from top to bottom and builds the page as it goes. When it reaches a <script> tag, it stops and runs that file before carrying on. That is why the examples here put the script at the end of the body: everything above it already exists, so the script can safely look for it.

Order matters between scripts too. The first file finishes before the second begins, and a name declared in one is visible to the other only when they share the same global scope. A script that runs before an element is parsed cannot find it — querySelector answers null, which is a quiet failure rather than a loud one.

Two kinds of failure

A syntax error is found before anything runs: a missing brace or quote means the browser cannot turn the file into instructions, so the whole script is skipped. A runtime error happens on a line that was actually reached — the lines before it have run, and the lines after it will not. That difference tells you where to look first.

The console is also literal about what it prints. A string appears without quotes, so console.log("5") and console.log(5) look identical in the pane. typeof is how you ask which one you have, and the answer is the subject of the next chapter.

The console is a tool, not the page

A console.log line is for you, not for the visitor. It appears in the developer tools, which most people never open, and it disappears when the tab closes. When something needs to be read on the page, the script has to find an element and write into it, which is what the later chapters do.

That distinction explains a common confusion: a script that logs the right answer can still look broken, because nothing visible changed. While you are building, the console is the faster place to check a value; when the program is finished, every message a visitor needs has to live in the document.

Three mistakes cost the most time here: using a name before it is declared, or misspelling it, throws ReferenceError; a selector that matches nothing returns null, and the next property access on it throws; and a script placed above the element it queries runs too early, so the page looks unchanged rather than broken. Read the first error line, not the last.

Try these next

  • Log typeof 5 and typeof "5", and compare the two answers in the console.
  • Declare a name, then misspell it on the next line. Note which line the error points at.
  • Remove a closing brace and run the script again. Nothing runs at all, because a syntax error stops the whole file.

Your first script

The script is empty. Log a line to the console, then declare your name with const and log a second line that uses it.

Live Preview
Console output will appear here...

2 left to do

  • The script logs a line
  • The writer is stored in a const

console.log("Hello from the page"); — then const name = "Ada"; and a second log that uses the name.

Three lines, in order

Log an opening line, store `6 * 7` in a const, log the answer, then log a closing line. Change the order and watch the console follow the file.

Live Preview
Console output will appear here...

3 left to do

  • The script logs a line
  • A value is stored before it is logged
  • The value is computed rather than typed

console.log("start"); — then const answer = 6 * 7; and console.log(answer);. The console lists the lines in the order they ran.

Every change in the editor re-runs the example, so the console is a live conversation: write a line, watch what it says, write the next one.

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What is JavaScript for, next to HTML and CSS?