console.log()
Prints values to the console for checking your work.
console.log("Total:", 42);Short entries with an example for each. When you need every detail, each entry links to the official documentation.
Prints values to the console for checking your work.
console.log("Total:", 42);Declare variables. Use const when the value never changes.
const tax = 0.08;
let total = 0;
total = 100 + 100 * tax;
console.log(total);Defines reusable code that can take inputs and return a result.
function area(w, h) {
return w * h;
}
console.log(area(3, 4));Runs code only when a condition is true, with an optional fallback.
let age = 15;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Not yet");
}Ordered lists of values. Indexes start at 0.
const colors = ["red", "green"];
colors.push("blue");
console.log(colors.length, colors[0]);Loops over every item in an array.
for (const fruit of ["apple", "pear"]) {
console.log(fruit);
}Group related values under names (properties).
const pet = { name: "Milo", age: 3 };
pet.age = 4;
console.log(pet.name, pet.age);Strings in backticks that can include values with a dollar sign and braces.
const name = "Ada";
console.log(`Hi, ${name}!`);Finds the first element that matches a CSS selector.
const title = document.querySelector("h1");
title.textContent = "Found it!";Runs a function when something happens, like a click.
button.addEventListener("click", () => {
button.textContent = "Clicked!";
});Reads or replaces the text inside an element.
document.querySelector("#msg").textContent = "Saved";Make a new element, then add it to the page.
const li = document.createElement("li");
li.textContent = "New item";
list.append(li);No entries match. Try a shorter word.