What is JavaScript?
JavaScript is the programming language of the web. It runs in every browser and makes HTML pages interactive — from form validation to complex apps.
🖱 Interactivity
Respond to user clicks, keyboard input, form submissions and mouse events.
🌐 DOM Control
Read and modify HTML and CSS on the fly — add elements, change styles, show/hide content.
🔗 APIs
Fetch data from servers, work with JSON, connect to third-party services.
⚙️ Server-side
With Node.js, JavaScript runs on servers too — full-stack in one language.
// Method 1: inline <script> tag <script> console.log("Hello, world!"); </script> // Method 2: external file (preferred) <script src="script.js"></script>
Variables
Variables store data. JavaScript has three ways to declare them — understanding when to use each is essential.
// let — can be reassigned let score = 0; score = 10; // ✅ OK // const — cannot be reassigned const MAX_SCORE = 100; // MAX_SCORE = 200; ❌ Error! // var — old way, avoid it var name = "Alice"; // has scoping issues
const by default. Switch to let only when you need to reassign. Never use var in modern code.Data Types
JavaScript has several built-in types. Understanding them prevents common bugs.
| Type | Example | Note |
|---|---|---|
| String | "Hello" / 'World' | Text. Use template literals for interpolation. |
| Number | 42 / 3.14 | All numbers (int and float) are the same type. |
| Boolean | true / false | Yes/No, on/off values. |
| Array | [1, 2, 3] | Ordered list of values. |
| Object | { name: "Bob" } | Key-value pairs. |
| null | null | Intentionally empty value. |
| undefined | undefined | Variable declared but not assigned. |
const name = "Alice"; const age = 25; // Old way: console.log("My name is " + name + " and I am " + age); // Modern: template literal with backticks console.log(`My name is ${name} and I am ${age}`);
Functions
Functions are reusable blocks of code. JavaScript has several ways to write them — arrow functions are the modern standard.
// Function declaration function greet(name) { return `Hello, ${name}!`; } // Arrow function (modern) const greet = (name) => `Hello, ${name}!`; // With default parameter const greet = (name = "World") => `Hello, ${name}!`; // Call the function console.log(greet("Alice")); // → "Hello, Alice!" console.log(greet()); // → "Hello, World!"
Conditionals
Conditionals let your code make decisions based on values. The if/else statement is the foundation of program logic.
const score = 72; // if / else if / else if (score >= 90) { console.log("A — Excellent!"); } else if (score >= 70) { console.log("B — Good"); } else { console.log("C — Keep practicing"); } // Ternary — compact version const result = score >= 70 ? "Pass" : "Fail";
Loops
Loops repeat code. The for loop and forEach method are the ones you'll use most.
// Classic for loop for (let i = 0; i < 5; i++) { console.log(`Step ${i + 1}`); } // while loop let count = 3; while (count > 0) { console.log(`Countdown: ${count}`); count--; } // forEach on array (most common) ["HTML", "CSS", "JS"].forEach(lang => { console.log(`Learning: ${lang}`); });
Arrays & Objects
Arrays store ordered lists; objects store named properties. Together they model almost any real-world data.
const skills = ["HTML", "CSS", "JavaScript"]; skills.push("React"); // add to end skills.pop(); // remove from end skills.length; // → 3 skills[0]; // → "HTML" // Powerful array methods const upper = skills.map(s => s.toUpperCase()); const web = skills.filter(s => s !== "HTML");
const user = { name: "Alice", age: 25, skills: ["HTML", "CSS"], greet() { return `Hi, I'm ${this.name}`; } }; console.log(user.name); // → "Alice" console.log(user["age"]); // → 25 console.log(user.greet()); // → "Hi, I'm Alice"
DOM & Events
The DOM (Document Object Model) is JavaScript's view of the HTML page. You can read and change it — and react to user events.
// Select elements const btn = document.getElementById("myBtn"); const title = document.querySelector("h1"); const cards = document.querySelectorAll(".card"); // Modify content and style title.textContent = "New Title"; title.style.color = "crimson"; btn.classList.add("active"); btn.classList.remove("hidden"); btn.classList.toggle("open");
const btn = document.getElementById("myBtn"); // Listen for a click event btn.addEventListener("click", () => { alert("Button clicked!"); }); // Input event — fires every keypress const input = document.getElementById("search"); input.addEventListener("input", (e) => { console.log(e.target.value); });
🛠 Project: Interactive Counter
Build a click counter that combines HTML, CSS and JavaScript — your first fully interactive web component!
<!-- HTML --> <div id="app"> <h2 id="count">0</h2> <button onclick="change(-1)">−</button> <button onclick="change(1)">+</button> <button onclick="reset()">Reset</button> </div> <!-- JavaScript --> <script> let n = 0; function change(delta) { n += delta; document.getElementById("count").textContent = n; } function reset() { n = 0; document.getElementById("count").textContent = n; } </script>
style.color inside the change() function.