Tutorial

JavaScript fundamentals: make pages do things

HTML describes things. CSS styles them. JavaScript makes them do things. If HTML is the nouns of a page and CSS the adjectives, JavaScript is the verbs: click this, fetch that, update this text.

What JavaScript actually is

A distinction most beginner tutorials skip, which causes confusion later. ECMAScript is the language standard, published as ECMA-262, with a new edition every year since 2015 (named by year: ES2015, ES2016, and so on). "JavaScript" is the name developers use for implementations of that standard. And the DOM, the thing that lets your code touch the page, is not part of the language at all. It is a browser API built around it. You write JavaScript; the browser hands you the DOM to work with.

Variables: named boxes for values

const siteName = "ParhoDev";   // will not change
let visitors = 0;              // will change
visitors = visitors + 1;

Use const by default and let when the value must change. You will see var in old tutorials; it is the pre-2015 way, and modern code does not use it.

Functions: reusable machines

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

greet("Sara");   // "Hello, Sara!"

A function takes inputs, does work, and returns a result. Name functions for what they do: calculateTotal, not doStuff. You will thank yourself in a month.

The DOM: the page as objects you can grab

The browser turns your HTML into a tree of objects called the Document Object Model. JavaScript can grab any element and change it:

const heading = document.querySelector("h1");
heading.textContent = "Changed by JavaScript!";

querySelector finds the element using the same selectors CSS uses. textContent reads or replaces its text. Prefer it over innerHTML when the text comes from users; innerHTML can execute injected markup, which is a security hole.

Events: pages that listen

Interactivity is just listening. Tell an element what to listen for and what to do when it happens:

const button = document.querySelector("#countBtn");
const output = document.querySelector("#count");
let count = 0;

button.addEventListener("click", function () {
  count = count + 1;
  output.textContent = count;
});

That is the whole pattern behind menus, tabs, forms, and sliders: select an element, listen for an event, run a function.

A complete working example

Save this as counter.html and open it in a browser. Everything in this tutorial, in one file:

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Counter</title>
</head>
<body>
  <h1>Button pressed <span id="count">0</span> times</h1>
  <button id="countBtn">Press me</button>

  <script>
    const button = document.querySelector("#countBtn");
    const output = document.querySelector("#count");
    let count = 0;

    button.addEventListener("click", function () {
      count = count + 1;
      output.textContent = count;
    });
  </script>
</body>
</html>

When it breaks: how to think

Your code will break constantly at first. That is normal; debugging is the actual job. Four habits that solve most beginner problems:

HabitWhy it works
Read the error message firstIt usually names the file and line number. Open DevTools with F12 and look at the Console tab before changing anything.
console.log everythingDo not guess what a variable holds; print it and look. Most "mysteries" end here.
Change one thing at a timeIf you change three things and it works, you learned nothing, and it will break again.
Remember code runs top to bottomHalf of beginner bugs are using something before it exists. If your script is in the head, the button does not exist yet when it runs; put scripts at the end of the body.

Another worked example: a character counter

Contact forms and comment boxes often show how many characters you have typed. The counter below updates as you type in a message field. It follows the same pattern as the button counter: select elements, listen for an event, update text.

<label for="message">Your message</label>
<textarea id="message" rows="4" cols="40"></textarea>
<p><span id="charCount">0</span> characters</p>

<script>
  const messageField = document.querySelector("#message");
  const countOutput = document.querySelector("#charCount");

  messageField.addEventListener("input", function () {
    countOutput.textContent = messageField.value.length;
  });
</script>

The HTML has two parts the script depends on. The textarea carries the id message, and the span that will show the number carries the id charCount and starts at 0.

The first line inside the script creates a variable named messageField and stores the textarea element in it. document.querySelector("#message") finds the element whose id is message. The value never changes, so the variable uses const.

The second line does the same for the output. countOutput stores the span element, so the code can change what that span displays without searching the page again each time.

The listener line attaches code to the textarea. addEventListener takes the event name "input" and a function. The browser fires an input event each time the content of the field changes, whether the change comes from typing, pasting, or deleting text.

The line inside the function does the update. messageField.value reads the current text in the textarea. Its length property gives the number of characters in that text. Assigning that number to countOutput.textContent replaces the text inside the span, so the number on the page matches the field as you type.

Try it in a file of your own. Type a sentence, delete part of it, then paste a longer paragraph. The count follows each change because the function runs again on every input event.

Frequently asked questions

Is JavaScript the same as Java?

No. The names rhyme because of 1990s marketing, not because of design. They are different languages for different jobs.

Do I need to learn all of JavaScript before building things?

No. Variables, functions, events, and basic DOM work cover most beginner projects. Build small things now; the rest arrives with use.

What is the difference between a library and a framework?

Both are other people's code that you reuse. You call a library when you want it; a framework calls your code and sets the rules. Learn plain JavaScript first either way, because both are built from it.

Last verified: October 22, 2026, against MDN Web Docs. The ECMAScript standard moves yearly; the fundamentals above do not.

Need interactivity without the learning curve?

Tutorials teach the craft. If you need the finished product, we build it.

Get a quote →

← All tutorials