You have met the two languages that build a page: HTML for structure and CSS for style. But a page built from those alone just sits there. To make it do something — count clicks, check a form, swap a picture when you tap it — you need a third language: JavaScript. It is the language that gives a web page a pulse.
The three layers of a web page
A modern page is a team of three languages, each with one job. HTML gives the structure (the headings, buttons and text). CSS gives the style (colours, fonts, layout). JavaScript gives the behaviour — what happens when the user does something. Think of a car: HTML is the body and seats, CSS is the paint and trim, and JavaScript is the engine that makes it move.
JavaScript runs in the browser (this is called client-side): the code is sent with the page and runs on your own device, so it can react to you instantly without asking a distant server first.
Variables: boxes that hold a value
A variable is a named store for a value the program wants to remember. In JavaScript you make one with let, as in let score = 7; — that puts the number 7 into a box named score. Later the program can read it, or change it with score = score + 1;. Variables can hold numbers, text (called a string, written in quotes like "Ali") and more.
Events: something happens, code runs
An event is something the user does that the page can react to: a click, a key press, a mouse hover. You tell the browser, "when this button is clicked, run this code." In HTML the simplest way is the onclick attribute: <button onclick="countUp()">Click me</button> runs a piece of code called countUp every time the button is clicked. The event is the trigger; the code is the response.
Changing the page: the DOM
The browser holds the whole page in memory as a tree of objects called the DOM (Document Object Model). JavaScript can reach into this tree and change it — edit some text, hide an element, change a colour — and the page updates on screen straight away. That is how a click can make the page different from how it started.
| Layer | Language | Job |
|---|---|---|
| Structure | HTML | What is on the page |
| Style | CSS | How it looks |
| Behaviour | JavaScript | What it does when used |
Tip: JavaScript is completely different from Java — the similar name is a historical accident. They are two separate languages, so do not mix them up in an exam answer.