JavaScript Basics: Variables, Functions, Events, and the DOM
Store values in variables, group steps into functions, react to clicks, and update the page. Covers reassignment and element selection, two things beginners often mix up.
Contents
Once HTML has placed the content and CSS has styled it, you often want something like "show the answer when the button is clicked." JavaScript is the language for changing values and the screen in response to what people do.
Here we connect the pieces one at a time: select an element on the page, react to an event, and update what is shown.
1. Give values a name
A variable is a named place to keep a value for later. Use const for names you will not reassign, and let for names whose value will change.
const topic = "HTML";
let questionCount = 0;
function makeMessage(name) {
return "Let's practice " + name;
}
questionCount = questionCount + 1;
console.log(makeMessage(topic), questionCount);With const you can still change what is inside an array or object, but you cannot point the name at a different value. When in doubt, start with const and switch to let when you need to update it.
2. Give a set of steps a name with functions
A function bundles several steps so you can run them whenever you need. The values you pass in are arguments, and the value it hands back is the return value.
function makeLabel(topic, level) {
return topic + ": level " + level;
}
const label = makeLabel("Grammar", 1);
console.log(label);Calling makeLabel("Grammar", 1) passes "Grammar" as topic and 1 as level, and returns the combined text. Defining a function does nothing by itself; it only runs when you call it.
3. Respond to actions with events
Things that happen in the browser, such as clicks and typing, are called events. addEventListener registers "when this happens on this element, run this code."
const button = document.querySelector("#show-answer");
const result = document.querySelector("#result");
button.addEventListener("click", () => {
result.textContent = "Answer: use h1 for the main heading";
});Here, a click on the button changes the text in result. The code that runs can be written inline as an anonymous function, or you can pass a function you defined elsewhere.
4. Update the screen through the DOM
The DOM is the set of page parts the browser builds after reading the HTML. querySelector picks the first element that matches a CSS selector, and setting textContent updates its text.
const score = document.querySelector("#score");
let points = 0;
function addPoint() {
points += 1;
score.textContent = "Points: " + points;
}
document.querySelector("#add").addEventListener("click", addPoint);For plain text, textContent is the easy choice. You change only the part you need instead of rebuilding the whole HTML every time.
A common mistake, and the fix
If the HTML uses id="result" but your JavaScript looks for message, nothing is found and you get null. Using a property on it then throws an error.
const output = document.querySelector("#message");
output.textContent = "Loaded";const output = document.querySelector("#result");
if (output) {
output.textContent = "Loaded";
}First, match the id to the HTML, and also handle the case where the element is missing. Code that selects elements must run after that HTML has loaded.
Where this site uses it
On the Chinese grammar map, the "Hide translations" checkbox changes how the study cards look. JavaScript receives the checkbox change and switches the display.
Read next
Continue with HTML basics for page structure and CSS basics for styling it.
Small experiments make it easier to see which line created a value and which line changed the screen. Start by making one button work.
⚠️ These notes were written for my own study. They do not guarantee accuracy or that you will pass an exam. Please don’t rely on them alone — check official sources and other materials yourself.