JavaScript for Beginners: Make Your Webpage Actually Do Things
Welcome to the most fun part of the trio. HTML is the skeleton. CSS is the paint. JavaScript is the brain. With JavaScript, your page stops being a document and becomes an application — menus open, forms validate, content updates without a refresh, things move on the screen.
Don't be scared of the word "programming." By the end of this article, you'll have written working JavaScript, understood every line, and built a small interactive component with your own hands. I promise.
What is JavaScript?
JavaScript (often shortened to "JS") is a programming language that runs in the browser. That's the big idea. The browser has a JavaScript engine built in. You give it instructions, it follows them. Done.
Unlike HTML and CSS (which just describe a page), JavaScript can decide things, remember things, and react to things. Want to show "Good morning!" before noon and "Good evening!" after? JavaScript. Want to load more results when the user scrolls to the bottom? JavaScript. Want to play a sound when the user clicks a button? JavaScript.
JavaScript is the only language that runs natively in every browser on every device. That makes it the most popular programming language in the world, and arguably the most important one to learn first.
Where do you write it?
Just like CSS, three options:
1. Inline — in an HTML attribute (avoid for anything but quick demos)
Click me
2. Internal — in a tag at the bottom of your HTML (fine for small things)
console.log("Hello from JavaScript!");
The console.log prints a message to the browser's developer console. Open DevTools (F12 or Cmd+Opt+I) to see it.
3. External — in a separate .js file (the right way)
Same pattern as CSS: one file, many pages, easier to maintain.
Variables: storing things in labeled boxes
A variable is a name you give to a value, so you can use it later. Think of it as a labeled box you put something in.
let name = "Hadi";
let age = 28;
let isOnline = true;
letmeans "create a new variable."nameis the label on the box."Hadi"is what's inside the box (a piece of text, called a "string" in programming).
Three flavors of variables in modern JavaScript:
let x = 5; // can be changed later
const y = 10; // cannot be changed (constant)
var z = 15; // old way — don't use this anymore
Use const by default. Switch to let only if you know the value will change. Never use var.
Functions: reusable bundles of instructions
A function is a recipe. You write the steps once, and you can run them any time with a name.
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("Hadi"); // prints: Hello, Hadi!
greet("Sara"); // prints: Hello, Sara!Read it in English: "Here's a function called greet. It takes a name, and it prints a greeting. Now run it twice, with two different names."
Modern JavaScript has a shorter way to write functions, called arrow functions:
const greet = (name) => {
console.log("Hello, " + name + "!");
};Does exactly the same thing. You'll see this style in modern code everywhere.
Conditionals: making decisions
Programs need to make decisions. The if statement is the way:
let hour = 14;
if (hour < 12) {
console.log("Good morning!");
} else if (hour < 18) {
console.log("Good afternoon!");
} else {
console.log("Good evening!");
}
"If it's before noon, say good morning. Otherwise, if it's before 6pm, say good afternoon. Otherwise, say good evening." Read it like English.
Arrays: lists of things
An array is a list:
let fruits = ["apple", "banana", "mango"];
console.log(fruits[0]); // apple
console.log(fruits[2]); // mango
console.log(fruits.length); // 3
Arrays are zero-indexed, meaning the first item is at position 0, not 1. This is the #1 thing beginners trip on. The first banana is at index 0. The second banana is at index 1. Got it? Good.
Objects: things with properties
An object is a collection of named values:
let user = {
name: "Hadi",
age: 28,
isAdmin: true
};
console.log(user.name); // Hadi
console.log(user.age); // 28Think of it as a form. Each property is a field on the form. You can access fields with a dot (user.name) or with brackets (user["name"]).
Loops: doing something many times
for (let i = 0; i < 5; i++) {
console.log("Step " + i);
}"Start at 0. While i is less than 5, run the code. After each run, add 1 to i." Output: Step 0, Step 1, Step 2, Step 3, Step 4.
The modern way to loop over an array is forEach or map:
fruits.forEach((fruit) => {
console.log("I like " + fruit);
});"For each fruit in the fruits list, print 'I like' followed by the fruit."
The DOM: talking to the HTML
Here is where JavaScript gets really powerful. The browser keeps a live model of your HTML page in memory. It's called the DOM (Document Object Model). JavaScript can read the DOM, change the DOM, and react when the DOM changes.
Three things you'll do constantly:
1. Find an element
const button = document.querySelector("button");
const allLinks = document.querySelectorAll("a");querySelector returns the first match. querySelectorAll returns all matches in a list.
2. Change it
button.textContent = "Don't click me";
button.style.color = "red";
button.classList.add("big");
You can change text, styles, classes — almost anything. The page updates instantly.
3. React to events
button.addEventListener("click", () => {
alert("You clicked it!");
});"When the user clicks this button, run this code." That's the heart of every interactive page on the web.
Putting it all together: a click counter
Let's build something real. In an HTML file, put:
Click Counter
body { font-family: system-ui; padding: 60px; }
button { padding: 12px 24px; font-size: 18px; cursor: pointer; }
.count { font-size: 48px; color: #ff8a3d; margin: 20px 0; }
How fast can you click?
0
Click me
let count = 0;
const countEl = document.getElementById("count");
const button = document.getElementById("btn");
button.addEventListener("click", () => {
count = count + 1;
countEl.textContent = count;
});
Save it, open it, click the button. The number goes up. You just built an interactive web app. Not a toy, not a fake — a real app that does something because of code you wrote.
Let me walk through what we did:
let count = 0— we created a variable to remember the count.document.getElementById("count")— we asked the browser "where is the element with id 'count'?" and got a reference to it.button.addEventListener("click", ...)— we told the browser: "when the user clicks this button, run this code."- Inside that code, we incremented the count and updated the page.
That's the entire pattern for almost every interactive feature on the web. Find elements. Listen for events. Update the page.
What to learn next
You now know the building blocks: variables, functions, conditionals, loops, arrays, objects, the DOM, and event listeners. With just those, you can build a huge amount of stuff.
Where to go from here?
- Practice more DOM stuff. Try a "to-do list" app: a text input, an "Add" button, a list below. Each time the user clicks Add, take the input's value, create a new
, and add it to the list. This is the classic beginner project for a reason. - Learn
fetch— how JavaScript talks to a server. With it, you can load data from an API, save form data, build a chat app. - Learn a framework. When your JavaScript files get big, frameworks like Vue.js, React, or Svelte help you organize them. Don't start here. Get comfortable with plain JavaScript first.
- Build things. The fastest way to learn is to build a small project every week. A countdown timer. A quiz. A weather widget. A color picker. Each one teaches you something new.
You started this article knowing nothing about programming. You finished it having built a real interactive web app. That's not a small thing. Be proud of yourself, and keep going.
The web is yours now. Go build something.
← More in Learn to Code