Javascript

JavaScript for beginners: variables, functions and the DOM

Avi, the Avenux Techspire mascot, illustrating: JavaScript for beginners: variables, functions and the DOM

HTML builds the page. CSS makes it look good. JavaScript makes it do things. When you click a button and a menu opens, or a form tells you your email is wrong before you submit it, that is JavaScript.

JavaScript is a big language, but three ideas take you a surprisingly long way: variables, functions and the DOM. Let’s go through each one.

Where to write JavaScript

The easiest way to start is a script tag at the bottom of your HTML body, pointing to a separate file:

<script src="script.js"></script>
</body>

Putting it at the end means the HTML has loaded before your script runs. You can also experiment directly in the browser console. Open developer tools, go to the Console tab, and type away.

Variables: storing values

A variable is a named box that holds a value. In modern JavaScript you create variables with let or const.

const courseName = "Web Development";
let seatsLeft = 10;

seatsLeft = seatsLeft - 1;
console.log(seatsLeft); // 9
  • const is for values you won’t reassign. Use it by default.
  • let is for values that will change, like a counter.
  • You will see var in older code. It has some confusing behaviour, so avoid it in new code.

Variables can hold different types of data:

const name = "Student";          // string
const age = 19;                // number
const isEnrolled = true;       // boolean
const skills = ["HTML", "CSS"]; // array
const student = { name: "Student", age: 19 }; // object

Functions: reusable blocks of code

A function is a set of instructions with a name. You write it once and run it whenever you need it.

function greet(name) {
  return "Hello, " + name + "!";
}

console.log(greet("world")); // Hello, world!
console.log(greet("friend")); // Hello, friend!

Here, name is a parameter, a placeholder for whatever value you pass in. return sends a result back to wherever the function was called.

You will also see arrow functions, a shorter way to write the same thing:

const greet = (name) => "Hello, " + name + "!";

Both work. Arrow functions are common in modern code, so get comfortable reading them, but there is no rush to use them everywhere.

The DOM: JavaScript meets the page

When the browser loads your HTML, it builds a tree of objects in memory, one for every element. This tree is the DOM, the Document Object Model. JavaScript can read and change this tree, and the page updates immediately.

Here is a small example. The HTML:

<p id="message">Click the button.</p>
<button id="btn">Click me</button>

And the JavaScript:

const message = document.querySelector("#message");
const button = document.querySelector("#btn");

let clicks = 0;

button.addEventListener("click", () => {
  clicks = clicks + 1;
  message.textContent = "You clicked " + clicks + " times.";
});

This uses all three ideas together:

  1. document.querySelector finds an element using a CSS selector, just like in your stylesheet.
  2. Variables hold references to those elements and the click count.
  3. addEventListener runs a function every time the button is clicked.
  4. textContent changes the text inside the paragraph.

Other useful DOM actions

// Add or remove a CSS class
menu.classList.toggle("open");

// Change a style directly
box.style.backgroundColor = "teal";

// Read what someone typed
const email = document.querySelector("#email").value;

Toggling a class is usually better than changing styles directly. Keep the look in CSS, and use JavaScript only to switch classes on and off.

Common beginner mistakes

  • Running the script before the HTML has loaded, so querySelector returns null
  • Forgetting the # for an id or the . for a class in the selector
  • Using = (assignment) when you meant === (comparison)
  • Not checking the console. Errors show up there with a line number. Read them.

Use console.log generously

When something doesn’t work and you can’t see why, print things. Add console.log(clicks) inside your function and watch the console. Is the function even running? Is the value what you expected? Most beginner bugs become obvious the moment you can see the actual values instead of guessing.

Small projects to try

  • A dark mode toggle button
  • A character counter under a text box
  • A simple to-do list where you add and remove items
  • A form that checks if the email field is empty before submitting

Each of these uses variables, functions and the DOM, and nothing else. Build all four, without copying a tutorial line by line, and you will understand JavaScript basics far better than if you had read ten more articles.