← Learning Hub
Beginner

JavaScript Introduction

JavaScript is the programming language of the web. HTML gives a page structure, CSS gives it style, and JavaScript makes it do things: respond to clicks, check forms, update content without reloading and much more.

JavaScript runs inside the browser. In a page, it goes inside a <script> element, usually just before </body> so the HTML above it has already loaded.

JavaScript is what makes websites feel like apps. When you tap "Add to cart" and the cart count changes without the page reloading, when a form tells you "invalid email" as you type, when a photo slider moves - that's JavaScript.

It's also the only programming language that every web browser understands, which makes it one of the most popular languages in the world. Today JavaScript also runs on servers (Node.js), in mobile apps (React Native) and in desktop apps, so learning it opens many doors.

JavaScript is a real programming language: it can store values, make decisions, repeat actions and react to events. The lessons that follow introduce each of these ideas one at a time.

Example
<!DOCTYPE html>
<html>
  <body>
    <h2 id="greeting">Hello!</h2>
    <button onclick="changeText()">Click me</button>

    <script>
      function changeText() {
        document.getElementById("greeting").textContent = "You clicked the button!";
      }
    </script>
  </body>
</html>
▶ Try it yourself

To see what your code is doing, open the browser's developer tools (press F12, or right-click and choose Inspect) and go to the Console tab. console.log(...) prints messages there - it's the most used debugging tool for every web developer.

Despite the similar name, JavaScript is not related to Java. They are completely different languages.
Key points
  • JavaScript adds behaviour to web pages.
  • It runs in the browser inside <script> tags.
  • Use the Console (F12) and console.log to see what's happening.
  • JavaScript and Java are different languages.

📝 Test yourself

1. What does JavaScript add to a web page?
2. Is JavaScript the same as Java?
3. Fill in the element that holds JavaScript in a page:
<>