After this lesson you will know what JavaScript is for, where it runs, and how it fits next to the HTML and CSS you already know. You will also read and edit your first script, a button that changes a sentence on the page.
Three languages, three jobs
A web page is built from three layers, and each one has its own language.
- HTML says what is on the page: a heading, a paragraph, a button, a form.
- CSS says how it looks: colours, spacing, fonts, layout.
- JavaScript says what happens: what the page does when someone clicks, types, scrolls or arrives.
Think of a house. HTML is the walls and rooms, CSS is the paint and furniture, and JavaScript is the wiring that makes the lights switch on when you press a switch. A page with no JavaScript is still a perfectly good page, and many pages should stay that way. You reach for JavaScript when the page needs to respond.
Typical jobs for JavaScript on a page:
- Show or hide a menu when a button is pressed.
- Check that a form is filled in correctly before it is sent.
- Load fresh data, such as new messages, without reloading the whole page.
- Calculate something, for example a total or a loan payment.
- Draw graphics, play audio, or build a small game.
It is a real programming language
JavaScript is a general programming language, not just a set of page tricks. It has variables, decisions, loops, functions and data structures, all of which you will meet in this course. The first part of the course, "Getting started", teaches the building blocks of the language itself. Later sections connect those blocks to the page.
A note on the name. JavaScript has nothing to do with Java. They are two separate languages with different rules, made by different people. The similar names are a historical accident, so do not assume that knowing one means you know the other.
Where JavaScript runs
JavaScript needs a program that can read it and carry it out. That program is called an engine, and it lives inside a bigger environment.
- Web browsers. Chrome, Firefox, Safari and Edge all contain an engine. In the browser, JavaScript can work with the page and respond to the visitor. The code runs on the visitor's own device, not on your server.
- Node.js. This is a runtime that lets you run JavaScript outside a browser. People use it for command-line tools, build tools and servers. There is no web page there, so page-related features are absent, but the language is the same.
- Other places. Desktop apps, mobile apps and some databases or cloud services also embed JavaScript. You do not need to learn any of them now.
This course focuses on the browser, because that is where you can see results immediately. A few lessons show output that Node.js would print in the same way, since the core language behaves identically in both.
One language, many versions
The language is defined by a standard called ECMAScript. A committee publishes a new edition roughly every year, and the editions are named by year, such as ES2015 or ES2024. When people say "modern JavaScript" they usually mean the style introduced from ES2015 onwards, with let, const, arrow functions and template literals. This course uses that style throughout.
Browsers and Node.js add their own extra features on top of the standard language. For example, document (the page) exists in browsers but not in Node.js. When a lesson uses something that only exists in one place, it will say so.
Your first script
Press the Try it button on the example below. Click the button inside the result, then edit the message in the code and see what changes.
<p id="message">Nothing has happened yet.</p>
<button id="go">Click me</button>
<script>
const message = document.getElementById("message");
const button = document.getElementById("go");
button.addEventListener("click", function () {
message.textContent = "JavaScript just changed this sentence.";
});
</script>You do not need to understand every word yet. Read it as a story in three steps:
- Find the paragraph and the button on the page, and give each a short name.
- Tell the button, "when you are clicked, run this little piece of code".
- That code replaces the text inside the paragraph.
HTML and CSS cannot do step 2 and 3 on their own. This is the gap that JavaScript fills. Every lesson from here on builds towards writing scripts like this, then larger ones.
How code is read
A JavaScript program is a list of instructions. The engine reads them from top to bottom, one after another, unless you tell it to skip or repeat some. Instructions usually end with a semicolon, and a line that starts with // is a comment that the engine ignores.
// This is a comment. The engine skips it.
const greeting = "Hello"; // store some text under a name
console.log(greeting); // show it in the consoleYou will use console.log constantly to look at values while you learn. The next lesson shows exactly how and where to find that output.
Common mistakes
- Mixing up Java and JavaScript. When you search for help, search for "JavaScript". Answers about Java will not apply.
- Expecting JavaScript to change the HTML file. A script changes the page that is open in the browser right now. The
.htmlfile on disk or on the server stays exactly as it was. - Thinking you must learn a framework first. React, Vue and similar tools are written in JavaScript. Learning the plain language first makes them far easier later.
- Typing code before the page has loaded. If a script looks for an element that is not on the page yet, it finds nothing. The next lesson explains how to place scripts so this does not happen.
- Treating copied code as finished learning. Pasting an example makes it work, but typing and changing it is how it sticks.
Exercise
In the editor above, make three changes and run the page after each one.
- Change the sentence that appears after the click.
- Change the button text from "Click me" to something else.
- Add a second paragraph with an
idof your choice, and change the script so that the click also updates that paragraph. Usedocument.getElementByIdthe same way the example does.
If something stops working, compare your code character by character with the original. Missing quotes and brackets are the usual cause. Then continue to Running JavaScript: script tags and the console.
Key points
- HTML describes the content of a page, CSS describes how it looks, and JavaScript describes how it behaves.
- JavaScript is a programming language that runs inside browsers, and also on servers and your own computer through runtimes such as Node.js.
- A script can read the page, change it, and react to things the visitor does, such as clicks and typing.
- JavaScript and Java are two unrelated languages that share part of a name for historical reasons.
- The language itself is standardised as ECMAScript, and browsers and Node.js each add their own extras on top.