{ }
JavaScript
for Beginners
Think Like a Programmer — not a copy-paste machine
Data-driven code • Reusable functions • Loops over duplication
A vibe-coding lesson • Vanilla JavaScript • Beginner friendly
1
What is JavaScript? (and why it exists)
🎯 Learning Objective: Understand what JavaScript is and why the web needs it.
Explanation
Code Example
// JavaScript reacting to a click
button.addEventListener("click", function () {
alert("Hello, world!");
});
💡 Important Note
Before JavaScript, web pages were static — like a printed poster. JS makes them respond.
▶ Expected Output
A small popup box appears on screen saying: Hello, world!
2
HTML vs CSS vs JavaScript
🎯 Learning Objective: Tell apart the three web languages and the job each one does.
Explanation
Code Example
<!-- HTML: structure -->
<button id="btn">Click me</button>
/* CSS: style */
#btn { background: gold; }
// JS: behavior
btn.onclick = function () { count++; };
💡 Important Note
This whole lesson focuses on JavaScript. Keep your HTML and CSS small and simple.
▶ Expected Output
A gold button that counts up every time you click it.
3
Variables: let vs const
🎯 Learning Objective: Store data in named boxes and choose between let and const.
Explanation
Code Example
let score = 0; // can change
score = 10; // OK
const name = "Ana"; // fixed
// name = "Ben"; // ERROR
💡 Important Note
Good names explain the value. Write price and score , not p and s .
▶ Expected Output
score becomes 10. Trying to change name would crash the program.
4
Data Types: String, Number, Boolean
🎯 Learning Objective: Recognize the three basic types of values in JavaScript.
Explanation
Code Example
const product = "Apple"; // String
const price = 20; // Number
const inStock = true; // Boolean
console.log(typeof price); // "number"
💡 Important Note
"20" with quotes is TEXT, not a number. Quotes change the type — be careful!
▶ Expected Output
The console prints: number
5
Data Types: Array & Object
🎯 Learning Objective: Store many values (array) or labeled values (object).
Explanation
Code Example
const fruits = ["Apple", "Orange"]; // array
const product = { // object
name: "Apple",
price: 20
};
💡 Important Note
An ARRAY OF OBJECTS (a list of things, each with details) powers most real apps.
▶ Expected Output
fruits holds 2 items; product describes 1 item with 2 details.
6
Arrays: create, access, add, remove
🎯 Learning Objective: Work confidently with lists of data.
Explanation
Code Example
const fruits = ["Apple", "Orange"];
console.log(fruits[0]); // "Apple"
fruits.push("Banana"); // add to end
fruits.pop(); // remove last
console.log(fruits.length); // 2
💡 Important Note
The FIRST item is index 0, not 1. This trips up every beginner at first!
▶ Expected Output
Prints "Apple" , then 2 (the length after adding & removing).
7
Objects: create, read, update
🎯 Learning Objective: Store and change labeled data about one thing.
Explanation
Code Example
const product = { name: "Apple", price: 20 };
console.log(product.name); // "Apple"
product.price = 25; // update
console.log(product.price); // 25
💡 Important Note
Use dot notation ( product.price ) to reach inside an object and grab a value.
▶ Expected Output
Prints "Apple" , then 25.
8
Functions: why they exist + declaration
🎯 Learning Objective: Package reusable instructions under one name.
Explanation
Code Example
function greet() {
console.log("Hello!");
}
greet(); // run it
greet(); // run it again
💡 Important Note
Functions are your #1 tool for avoiding copy-pasted, duplicated code.
▶ Expected Output
Prints Hello! twice.
9
Functions: parameters, return, calling
🎯 Learning Objective: Pass data in, and get a result back out.
Explanation
Code Example
function addTax(price) {
return price + (price * 0.12);
}
const total = addTax(100);
console.log(total); // 112
💡 Important Note
One addTax() works for every price — no need for addTax100(), addTax200()...
▶ Expected Output
Prints 112.
10
Loops: the for loop
🎯 Learning Objective: Repeat code a set number of times without copy-pasting.
Explanation
Code Example
const fruits = ["Apple", "Orange", "Banana"];
for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
💡 Important Note
The condition i < fruits.length stops the loop safely at the end of the list.
▶ Expected Output
Prints Apple , Orange , Banana (one per line).
11
Loops: for...of and forEach()
🎯 Learning Objective: Loop through items with simpler, cleaner syntax.
Explanation
Code Example
const fruits = ["Apple", "Orange"];
for (const fruit of fruits) {
console.log(fruit);
}
fruits.forEach(function (fruit) {
console.log(fruit);
});
💡 Important Note
Loops turn 100 lines of copy-paste into just a few lines. That's the whole point!
▶ Expected Output
Each block prints Apple , then Orange.
12
DOM: getElementById & querySelector
🎯 Learning Objective: Grab HTML elements so JavaScript can control them.
Explanation
Code Example
const title = document.getElementById("title");
const firstBtn = document.querySelector("button");
const card = document.querySelector(".card");
💡 Important Note
querySelector uses CSS style: # for an id, . for a class.
▶ Expected Output
Each variable now holds a real element taken from the page.
13
DOM: innerHTML vs textContent
🎯 Learning Objective: Read and change what is inside an element.
Explanation
Code Example
title.textContent = "Welcome!";
list.innerHTML =
"<li>Apple</li><li>Orange</li>";
💡 Important Note
This is how JS builds page content FROM DATA — instead of hardcoding HTML by hand.
▶ Expected Output
The heading shows Welcome! ; the list shows two bullet items.
14
Events: onclick & addEventListener
🎯 Learning Objective: Make code run in response to what the user does.
Explanation
Code Example
btn.onclick = function () {
console.log("clicked!");
};
btn.addEventListener("click", function () {
console.log("clicked again!");
});
💡 Important Note
Prefer addEventListener — you can attach many listeners to one element.
▶ Expected Output
Each click prints a message to the console.
15
Putting Everything Together
🎯 Learning Objective: Combine array + object + loop + function + DOM in one demo.
Explanation
Code Example
const products = [
{ name: "Apple", price: 20 },
{ name: "Orange", price: 15 }
];
function makeRow(p) {
return `<li>${p.name} — ₱${p.price}</li>`;
}
let html = "";
products.forEach(function (p) {
html += makeRow(p);
});
list.innerHTML = html;
💡 Important Note
The `${...}` inside backticks is a TEMPLATE LITERAL — it drops data into text. This is exactly the pattern you'll use in the activity!
▶ Expected Output
A list on the page: Apple — ₱20 , Orange — ₱15.
{ }
🚀 Your Turn: Build a Mini Product Store
Store products as DATA, then let JavaScript build the page. No hardcoded HTML!
What you'll build
Your code must include
Full instructions, starter files & the 100-point rubric are in ACTIVITY.md