Make your websites actually do stuff
Put this at the bottom of your HTML, just before </body>:
<script>
// Your JavaScript here
</script>
Or link an external file: <script src="script.js"></script>
JavaScript runs as soon as it loads. If it's in the <head>, it runs before the page content exists. Put it at the bottom so the HTML is ready first.
const by default, let when you need to change it.const name = "Jaclyn"; // Can't be changed
let score = 0; // Can be changed
var old = "don't use"; // Old way, avoid it
const text = "Hello"; // String (text)
const num = 42; // Number
const decimal = 3.14; // Also a number
const isOn = true; // Boolean (true/false)
const empty = null; // Intentionally empty
const mystery; // undefined (no value yet)
const colors = ["red", "green", "blue"];
colors[0]; // "red" (first item, index 0)
colors[2]; // "blue" (third item)
colors.length; // 3
colors.push("yellow"); // Add to end
colors.pop(); // Remove from end
const person = {
name: "Jaclyn",
age: 30,
city: "Chicago"
};
person.name; // "Jaclyn"
person["age"]; // 30 (alternate syntax)
person.job = "Designer"; // Add new property
function sayHello(name) {
return "Hello, " + name;
}
sayHello("Jaclyn"); // "Hello, Jaclyn"
const sayHello = (name) => {
return "Hello, " + name;
};
// Even shorter for one-liners:
const double = (n) => n * 2;
if (score > 90) {
console.log("A");
} else if (score > 80) {
console.log("B");
} else {
console.log("Keep trying");
}
x === y // Equal (use this one!)
x !== y // Not equal
x > y // Greater than
x < y // Less than
x >= y // Greater or equal
x <= y // Less or equal
// Combine with:
&& // AND (both must be true)
|| // OR (either can be true)
! // NOT (flips true/false)
Always use === (triple equals). It checks value AND type. Double equals == does weird conversions and causes bugs.
for (let i = 0; i < 5; i++) {
console.log(i);
}
const fruits = ["apple", "banana", "cherry"];
for (const fruit of fruits) {
console.log(fruit);
}
fruits.forEach((fruit) => {
console.log(fruit);
});
// Get one element
const btn = document.querySelector(".my-button");
const header = document.querySelector("#main-header");
// Get multiple elements
const items = document.querySelectorAll(".list-item");
// Change text
element.textContent = "New text";
// Change HTML inside
element.innerHTML = "<strong>Bold text</strong>";
// Direct style
element.style.color = "red";
element.style.backgroundColor = "#000";
// Add/remove CSS class (better)
element.classList.add("active");
element.classList.remove("active");
element.classList.toggle("active");
// When button is clicked
btn.addEventListener("click", () => {
console.log("Button clicked!");
});
// Common events:
// "click" - mouse click
// "submit" - form submitted
// "keydown" - key pressed
// "mouseover" - hover
// "change" - input value changed
const btn = document.querySelector("#dark-mode-btn");
btn.addEventListener("click", () => {
document.body.classList.toggle("dark");
});
const menu = document.querySelector(".menu");
const toggle = document.querySelector(".menu-toggle");
toggle.addEventListener("click", () => {
menu.classList.toggle("hidden");
});
const input = document.querySelector("#email");
const form = document.querySelector("form");
form.addEventListener("submit", (e) => {
e.preventDefault(); // Stop page refresh
const email = input.value;
console.log("Email:", email);
});
Use console.log() constantly to see what your code is doing. Open browser DevTools (F12) → Console tab to see the output. It's how you debug.
This covers the basics. When you're ready for more: MDN Web Docs (mozilla.org) is the best free reference. Search "MDN [whatever you need]".