JavaScript Introduction
JavaScript is the programming language of the web. HTML gives a page its structure and CSS gives it style, but JavaScript makes it do things: react to clicks, check a form before it is sent, load new data without refreshing, and much more. Every modern browser can run it, so it is one of the most useful languages you can learn.
What is JavaScript?
JavaScript was created by Brendan Eich at Netscape in 1995 to add simple interactivity to web pages. Despite the name, it has almost nothing to do with Java; the name was chosen for marketing reasons at a time when Java was fashionable. The language is standardised under the name ECMAScript. The 2015 edition (often called ES6) was a huge update that added let, const, arrow functions, classes and modules, and a new edition has been released every year since. This series teaches modern JavaScript (ES2023 and later), the version all current browsers support.
JavaScript is a high-level, dynamically typed language. You don't manage memory yourself, and a variable can hold any kind of value. If you have learned Python, much will feel familiar, although JavaScript uses curly braces { } for blocks instead of indentation.
The three languages of the front end
A useful way to remember who does what on a web page:
| Language | Job | Example |
|---|---|---|
| HTML | Content and structure | A heading, a paragraph, a button |
| CSS | Presentation | The button is yellow with rounded corners |
| JavaScript | Behaviour | Clicking the button adds an item to your cart |
With JavaScript you can show and hide menus, validate phone numbers on a sign-up form, build games, draw charts, play audio, fetch exchange rates from an API and save a user's settings in the browser. Apps you use daily, from online banking dashboards to chat apps, rely on it heavily.
Where JavaScript runs
Your JavaScript code is executed by a JavaScript engine. Every browser ships one: V8 in Chrome and Edge, SpiderMonkey in Firefox and JavaScriptCore in Safari. The engine understands the language itself (variables, loops, functions). The browser then adds extra tools called Web APIs, such as the DOM for changing the page, events for reacting to the user and fetch for talking to servers.
In 2009 Ryan Dahl released Node.js, which takes the V8 engine out of the browser so JavaScript can run on servers and laptops. Node.js has no web page and therefore no DOM, but it adds its own tools for reading files, handling network requests and installing packages with npm. That is why one language can power both the page you see and the server behind it.
A first taste
Here is a complete web page with a little JavaScript at the bottom. When you click the button, the script finds the heading and changes its text. Press Try it yourself, click the button, then change the words and run it again.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My first script</title>
</head>
<body>
<h1 id="greeting">Habari!</h1>
<button id="translate">Translate to English</button>
<script>
const heading = document.getElementById("greeting");
const button = document.getElementById("translate");
button.addEventListener("click", () => {
heading.textContent = "Hello!";
});
</script>
</body>
</html>
Don't worry about every word yet. document.getElementById finds an element, addEventListener waits for a click, and textContent changes the text. You will learn each piece in the DOM chapter.
JavaScript can also simply calculate and print results. The console.log() function writes a message to the browser's console, a panel developers use to check what their code is doing:
const name = "Amina";
const year = 2026;
console.log("Hello, " + name + "!");
console.log("JavaScript turned", year - 1995, "this year.");
Notice that console.log puts a space between values separated by commas. Each statement ends with a semicolon. JavaScript can often insert missing semicolons for you, but writing them yourself avoids a few confusing bugs.
JavaScript is case-sensitive. console.log works, but Console.log or console.Log causes an error. The same goes for your own names: total and Total are two different variables.
How to learn JavaScript well
Type the examples yourself instead of only reading them, and change them to see what happens. Keep the browser console open (the next pages show you how), because it tells you exactly what went wrong when code fails. Work through the chapters in order: the basics (variables, types, conditions and loops) come first, then functions, objects and arrays, and then the exciting part, controlling real web pages.
Key points
- JavaScript adds behaviour to web pages; HTML is structure and CSS is style.
- It was created in 1995, is standardised as ECMAScript and gets a new edition every year.
- Browsers run it with an engine (such as V8) plus Web APIs like the DOM; Node.js runs it outside the browser.
console.log()prints values to the console, your main tool for seeing what code does.- JavaScript is case-sensitive and uses curly braces for blocks.
Tip: use the ← and → keys to move between pages.
