Skip to content
Happy Programming Guide
Start learning
JavaScript

Introduction to JavaScript

What JavaScript is, where it runs, what it is used for, and how it relates to HTML and CSS — plus your first working script in the browser.

A pen and an open notebook ready for notes

JavaScript is the language that makes web pages do things. HTML provides the content, CSS controls how it looks, and JavaScript responds to what people do — clicking, typing, scrolling — and changes the page in response. It also runs on servers, in mobile apps and on small devices, but the browser is where nearly everyone meets it first.

The three languages of a web page#

Every web page you have ever visited is built from three things:

  • HTML is the content and structure — headings, paragraphs, images, buttons.
  • CSS is the presentation — colours, spacing, layout, fonts.
  • JavaScript is the behaviour — what happens when someone interacts with it.

A page with only HTML works fine; it is just static. Add CSS and it looks designed. Add JavaScript and it can respond, fetch new data, validate a form before it submits, or update a total as you change a quantity.

Your first script#

Save this as index.html and open it in a browser:

HTML
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>First script</title>
</head>
<body>
  <h1 id="greeting">Hello</h1>
  <button id="change">Change the heading</button>

  <script>
    const heading = document.querySelector("#greeting");
    const button = document.querySelector("#change");

    button.addEventListener("click", () => {
      heading.textContent = "You clicked it at " + new Date().toLocaleTimeString();
    });
  </script>
</body>
</html>

Click the button and the heading changes. That is the whole pattern of browser JavaScript in five lines: find an element, listen for something, change something.

The console#

Press F12 in any browser and choose Console. You can type JavaScript there and see the result immediately — no file, no saving, no reloading.

JavaScript
2 + 2
"hello".toUpperCase()
document.title
[1, 2, 3].map(n => n * 10)

The console is also where error messages appear. When a page “does not work”, the console is the first place to look, and it usually names the file and line.

JavaScript
console.log("value is", value);
console.table(arrayOfObjects);

Where JavaScript runs#

Where What it does
Browser Page behaviour, forms, animation, fetching data
Node.js Servers, APIs, command-line tools, build scripts
Mobile Apps via React Native and similar frameworks
Desktop Applications via Electron and Tauri
Devices Small hardware projects and automation

Same language, different surroundings. Browser JavaScript can touch the page but not your files; Node can read files but has no page. The core language — variables, functions, loops, objects — is identical.

What the language looks like#

JavaScript
// Variables
const name = "Ana";        // cannot be reassigned
let count = 0;             // can be reassigned

// Functions
function double(n) {
  return n * 2;
}

const triple = (n) => n * 3;   // arrow function, same idea

// Decisions
if (count > 10) {
  console.log("lots");
} else {
  console.log("not many");
}

// Loops
for (const item of ["a", "b", "c"]) {
  console.log(item);
}

// Objects and arrays
const person = { name: "Ana", age: 34 };
const scores = [88, 72, 95];

If you have seen Python, most of this will feel familiar — the main visible differences are the braces, the semicolons and const versus let.

Changing the page#

JavaScript
// Find things
const title = document.querySelector("h1");
const items = document.querySelectorAll(".item");

// Change them
title.textContent = "New heading";
title.classList.add("highlight");
title.style.color = "crimson";

// Respond to things
document.querySelector("form").addEventListener("submit", (event) => {
  event.preventDefault();     // stop the page reloading
  console.log("form submitted");
});

The collection of objects representing the page is called the DOM — the Document Object Model. Most browser JavaScript is reading from it and writing to it.

Fetching data#

JavaScript
async function loadUsers() {
  const response = await fetch("https://jsonplaceholder.typicode.com/users");
  if (!response.ok) {
    throw new Error("Request failed: " + response.status);
  }
  const users = await response.json();
  console.log(users[0].name);
}

loadUsers();

This is what lets a page update without reloading. It is also where JavaScript first feels genuinely powerful, so it is worth reaching reasonably early.

What to learn, in order#

  1. Variables and types — const, let, strings, numbers, booleans
  2. Functions — including arrow functions, since you will see them everywhere
  3. Arrays and objects — nearly all real data is one or the other
  4. The DOM — finding and changing elements
  5. Events — responding to clicks, input and submissions
  6. Async and fetch — getting data from elsewhere
  7. Modules — splitting code across files

Frameworks such as React, Vue and Svelte come after all of that. They are built on these ideas, and learning them first makes the framework far easier.

Questions people ask#

Do I need to learn HTML and CSS first?

Enough HTML to build a page with a few elements, yes. Deep CSS can wait. JavaScript that changes a page needs a page to change.

What is ECMAScript?

The official name of the standard JavaScript implements. Version names like ES6 or ES2015 refer to it. In everyday conversation the two words are used interchangeably.

Do I need to install anything?

Not to start. A browser and a text editor are enough. Install Node.js when you want to run JavaScript outside a page or use npm packages.

Is JavaScript still worth learning?

It is the only language browsers run, so anything that happens in a web page goes through it. That alone makes it one of the most widely used languages there is.

Where to go next#

JavaScript basics, with the syntax you will use dailyRead next

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

Your email address will not be published. Required fields are marked *