Beginner · 9 min read · Updated 2026-11

JavaScript basics for the web

The language that runs in every browser — including this site. Variables, functions, and making a page react.

JavaScript is the only programming language that runs natively in web browsers, which makes it the language of interactive websites. Everything that moves on this very page is JavaScript. You can try it right now: open your browser's developer tools (F12) and type into the Console.

Variables and text

Use let for a value that can change and const for one that can't. Text (a “string”) goes in quotes, and backticks let you drop values straight into text with ${…}.

javascript
const name = "Sepehr";
let visits = 3;
visits = visits + 1;
console.log(`${name} visited ${visits} times`);
Logs: Sepehr visited 4 times

Functions

A function is a named block of code that takes inputs and gives back a result. It lets you write something once and use it many times.

javascript
function area(width, height) {
  return width * height;
}
console.log(area(4, 5)); // 20

Making the page react

JavaScript's real job in a browser is to respond to the user. You find an element on the page and tell it what to do when something happens — here, when a button is clicked.

javascript
const button = document.querySelector("#hello");
button.addEventListener("click", () => {
  alert("You clicked me!");
});
When #hello is clicked, a message pops up.
Everything on this website — the page turning in the book, the chat assistant, the language switch — is plain JavaScript, no framework. View the source to see it.
All articles