KodingKaro
JavaScript · Beginner · Introduction to JavaScript

What JavaScript does on a web page

JavaScript adds decisions and behavior to web pages. This lesson separates the jobs of HTML, CSS, and JavaScript and follows a small browser interaction.

✓ What you will learn in this topic

  • Describe the roles of HTML, CSS, and JavaScript.
  • Explain why JavaScript differs from Java.
  • Identify browser APIs as environment features.

Concept explanation

HTML describes page content, CSS describes its appearance, and JavaScript responds to events or changes data. The language is called JavaScript; despite the similar name, Java is a separate language with different syntax, tools, and runtime history.

Knowing which layer owns a behavior helps keep projects understandable. For example, an HTML button remains a button, CSS styles it, and JavaScript decides what happens when someone activates it.

Syntax

A browser script can select an element, register a function for an event, and update the page. The DOM is the browser’s in-memory representation of HTML.

Example 1: a small experiment

This language-only example stores text and sends it to the developer console. It runs in a browser console or Node.js.

const greeting = 'Welcome to the course';
console.log(greeting);

Expected output: The console displays Welcome to the course.

Why it is useful: Logging a small value is a quick way to confirm that code ran and to inspect data.

Example 2: a practical feature

This browser example uses document to find HTML elements and changes the tip only after the button is clicked.

const button = document.querySelector('#show-tip');
button.addEventListener('click', () => {
  document.querySelector('#tip').textContent = 'Save your work often.';
});

Expected output: Clicking the button changes the tip paragraph to “Save your work often.”

Why it is useful: Event-driven behavior is the basis of menus, dialogs, forms, and other interactive features.

Common mistakes

JavaScript is not Java, and it does not replace HTML or CSS. Browser JavaScript has web-page APIs such as document; Node.js has server-side APIs and no document by default.

Best practices

Use an external script with defer for page behavior, and use the browser console to inspect errors and experiment without changing the source file.

Practice exercise

Create a page with a button and a paragraph. When clicked, replace the paragraph text with a short welcome message. Identify which part is HTML and which lines provide behavior.

Lesson summary

JavaScript supplies behavior; the browser provides page APIs such as document and event listeners.

Knowledge check

Which language layer should respond when a visitor clicks a button?

Show explanation

HTML creates the button, CSS styles it, and JavaScript can respond to its click.

Practical example

javascript
const button = document.querySelector('#show-tip');
button.addEventListener('click', () => {
  document.querySelector('#tip').textContent = 'Save your work often.';
});

Expected result:

Clicking the button changes the tip paragraph to “Save your work often.”

Hands-on practice task

Create a page with a button and a paragraph. When clicked, replace the paragraph text with a short welcome message. Identify which part is HTML and which lines provide behavior.

In this section

Other lessons in Introduction to JavaScript

Broaden your knowledge

Related learning topics