Chapter Six · Behaviour

JavaScript: First Steps

The first programming in the book: what JavaScript is for in a page, how a script joins the site, the values and names every program is built from, decisions, functions, and how to read the errors that tell you what went wrong.

Up to now the site has held still: HTML gave it meaning and CSS gave it a look, but nothing on it answers the visitor. JavaScript is the third language, the one that gives a page behaviour, and it is also a real programming language. This chapter is where you learn to program, from the very first value, in the Console you already know from DevTools. Nothing here changes what the visitor sees yet; the next three chapters build on these pieces until the page answers clicks and keys.

Five topics. What JavaScript does in a page, how the site loads its script with defer, and the Console where a script talks to you. Values, their types and the names that point at them, with const, let and template literals. Comparisons and decisions, including the two traps that catch almost every beginner. Functions: named pieces of work with inputs and a returned result. And reading errors and using the debugger, the habit that saves more time than any other.

What a script in a page does, in one line
The page loadsHTML and CSS first
→
Your script runstop to bottom, once
→
It waitsfor something to happen
→
It changes the pagewhat the visitor sees

Topics in This Chapter