JavaScript Foundations – Variables and Data Types
Welcome to JavaScript
You may have already built pages with HTML and styled them with CSS. Your pages look good — but they just sit there. They don't react, they don't calculate, they don't change. That's about to change.
JavaScript is the programming language of the web. It's the layer that gives web pages a brain — it lets them respond to clicks, process data, update content in real time, validate forms, build games, create charts, and communicate with servers. Every website you interact with dynamically — Gmail, Instagram, Google Maps, Twitter — runs on JavaScript.
The best part? JavaScript runs directly in the browser. You don't need special software to run it — just a text editor and a browser you already have. You can start writing programs today.
console.log() — and you'll write your first real JavaScript program that calculates and compares values.
The Three Layers of the Web
Every webpage is built from three languages working together. Think of building a house:
The raw frame and materials of a house — walls, floors, doors, windows. HTML defines what elements exist on the page: headings, paragraphs, images, buttons, links, forms.
Welcome
Hello, world!
The paint, wallpaper, furniture, and layout. CSS controls how things look: colors, fonts, sizes, spacing, animations, and screen layout.
h1 { color: navy; font-size: 2em; }
button { background: green; color: white; }
The electricity, plumbing, and smart home systems. JavaScript makes the page respond, calculate, make decisions, and change over time — without reloading.
let price = 49.99;
let tax = price * 0.08;
let total = price + tax;
How All Three Work Together
What exists
How it looks
What it does
Fully interactive
What JavaScript Does in the Browser
JavaScript has four core superpowers in the browser. Everything you'll ever build with JS falls into one of these categories:
JavaScript can read and change anything on the page — headings, paragraphs, images, buttons — without a reload.
// Change the text of an element
document.querySelector('h1').textContent = 'Welcome back, Alice!';
// Change a style
document.querySelector('body').style.background = '#000';
Every click, keypress, scroll, or mouse hover can trigger JavaScript code to run.
// Run code when user clicks a button
button.addEventListener('click', function() {
alert('You clicked the button!');
});
JavaScript performs arithmetic, text manipulation, date calculations, and logic — acting as the brain of your app.
// Shopping cart calculation
let price = 29.99;
let quantity = 3;
let discount = 10; // 10% off
let subtotal = price * quantity;
let total = subtotal - (subtotal * discount / 100);
// total = 80.97
JavaScript uses conditions to choose different paths — showing different content based on user data, login state, time of day, and more.
// Show different messages based on score
let score = 78;
if (score >= 90) {
message = "Excellent! Grade: A";
} else if (score >= 70) {
message = "Good work! Grade: B";
} else {
message = "Keep practising. Grade: C";
}
How to Link JavaScript to HTML
Like CSS, JavaScript can be added to HTML in three ways. The external file approach is the professional standard and what you should always use for real projects.