JavaScript is the language that makes web pages interactive. If HTML is the structure of a page and CSS is its appearance, JavaScript is the behaviour: opening menus, validating forms, loading data without a refresh. This tutorial walks through the fundamentals you need before moving on to frameworks, with short examples you can paste into your browser console and run right now.
It is written from the point of view of someone who uses JavaScript every day in production React and Angular applications, so I have focused on the parts you will genuinely use rather than every corner of the language.
How to run JavaScript
You do not need to install anything. Open any page in your browser, press F12, and choose the Console tab. Anything you type there runs immediately. To use JavaScript in a real page, add a script tag at the end of your HTML:
<script src="app.js" defer></script>
The defer attribute tells the browser to download the file while it parses the page and run it afterwards, so your script can safely find elements on the page.
Variables: let, const and var
Variables store values. Modern JavaScript has two keywords you should use: const for values that will not be reassigned and let for values that will. Avoid var, which has confusing scoping rules and exists mostly for old code.
const name = "Asha"; // cannot be reassigned
let score = 0; // can change
score = score + 10;
console.log(name, score); // Asha 10
A good habit: start with const and switch to let only when you find you need to reassign.
Data types
JavaScript has a small set of core types: strings, numbers, booleans, null, undefined, objects (which include arrays and functions) and a few newer ones such as BigInt and Symbol. You can check a value with typeof.
typeof "hello"; // "string"
typeof 42; // "number"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof []; // "object" (arrays are objects)
Always use strict equality
Use === rather than ==. The double-equals operator converts types before comparing, which produces surprises such as "5" == 5 being true. Strict equality compares both value and type.
Functions
A function is a reusable block of code. You can declare one with the function keyword or write a shorter arrow function.
function add(a, b) {
return a + b;
}
const multiply = (a, b) => a * b;
console.log(add(2, 3)); // 5
console.log(multiply(4, 5)); // 20
Functions can have default parameters (function greet(name = "friend")) and can be passed around like any other value. That second idea, functions as values, is the foundation of almost everything in modern JavaScript.
Conditions and loops
Use if, else if and else to make decisions, and loops to repeat work.
const age = 20;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}
for (let i = 1; i <= 3; i++) {
console.log("Round", i);
}
Arrays and the methods you will use daily
An array holds an ordered list of values. Rather than writing loops by hand, learn three methods: map transforms every item, filter keeps items that pass a test, and reduce combines everything into one value.
const prices = [120, 80, 45, 200];
const withTax = prices.map((p) => p * 1.18);
const expensive = prices.filter((p) => p > 100);
const total = prices.reduce((sum, p) => sum + p, 0);
console.log(expensive); // [120, 200]
console.log(total); // 445
These methods return new arrays instead of changing the original, a habit that will matter a great deal when you learn React state.
Objects
Objects group related data using key and value pairs. You will see them everywhere, especially as the shape of data returned from APIs.
const user = {
name: "Asha",
role: "developer",
skills: ["HTML", "CSS", "JavaScript"],
};
console.log(user.name); // "Asha"
console.log(user.skills[2]); // "JavaScript"
const { name, role } = user; // destructuring
const updated = { ...user, role: "senior developer" }; // copy with a change
Working with the DOM
The Document Object Model (DOM) is the browser’s live representation of your HTML. JavaScript can read it, change it and react to user actions. Here is a button that updates a counter:
<button id="inc">Clicked 0 times</button>
<script>
const button = document.querySelector("#inc");
let count = 0;
button.addEventListener("click", () => {
count += 1;
button.textContent = `Clicked ${count} times`;
});
</script>
querySelector finds an element with a CSS selector, addEventListener runs your function when something happens, and textContent changes what is displayed. Almost every interactive feature on the web is a variation of those three steps.
Fetching data
Real applications load data from servers. The fetch API returns a promise, and async and await let you write that code in a readable top-to-bottom style. Always handle errors.
async function loadUser(id) {
try {
const res = await fetch(`https://jsonplaceholder.typicode.com/users/${id}`);
if (!res.ok) throw new Error(`Request failed: ${res.status}`);
const user = await res.json();
console.log(user.name);
} catch (err) {
console.error(err.message);
}
}
loadUser(1);
Common beginner mistakes
- Using
==instead of===and getting surprising results. - Running a script before the page elements exist; use
deferor place the script at the end of the body. - Forgetting that
constarrays and objects can still be modified; it only stops reassignment. - Ignoring errors from
fetch. A 404 does not throw by itself, so checkres.ok. - Copying code without reading it. Type examples out and change them to see what breaks.
What to learn next
Practise by building three small things: a to-do list that adds and removes items, a form with validation, and a page that fetches and displays data from a public API. Once those feel comfortable, learn modules (import and export), then pick a framework. If you are heading to React, continue with React Hooks explained with practical examples. To make what you build look good on every screen, read Responsive Web Design Using HTML and CSS.
For deeper reference, the MDN JavaScript documentation is the best free resource available, and javascript.info is an excellent structured course.