HexoQuest

Free · no sign-up · runs in your browser

JavaScript tutorial for teens

Learn real JavaScript in 9 short, interactive lessons. Read a little, change the code, press Run and see the result right away. Every lesson checks your answer, so you always know if you got it.

For ages 13–17 and total beginners. About 60 minutes. Your progress is saved in this browser.

Progress: 0 / 9 lessons solved

Hexo the robot coding on a laptop

What you will learn

JavaScript is the language of the web. It runs in every browser, on every phone and laptop, and it is what makes web pages react when you click, type or play. Games in the browser, chat apps, YouTube's buttons: all JavaScript. That makes it one of the best first languages for teens. You don't need to install anything, and whatever you build, you can show your friends with a link.

This JavaScript tutorial for teens starts from zero. You will:

Each lesson has a short explanation, an example and a challenge. Edit the code in the box and press ▶ Run. The page preview and the console show what your code did, and the checks tell you what is still missing. Stuck? Open the hint. Still stuck? Peek at the solution, then try to write it again without looking.

Lesson 1 of 9

Your first line of JavaScript

Programmers talk to themselves through the console. Whatever you put inside console.log( ) appears there. Text always goes between quotes. Every browser has a console: press F12 on any web page and open the Console tab.

console.log("Hi there");
console.log(2 + 3);   // numbers don't need quotes

Your challenge

Log exactly Hello, world! to the console. Watch the comma and the exclamation mark.

💡 Hint

Change the text between the quotes: console.log("Hello, world!");

👀 Show the solution
console.log("Hello, world!");

🎮 Practise the same idea in the game: JavaScript & the Web, level 1 (free).

Console
Press ▶ Run to see the output.

    Lesson 2 of 9

    Variables: give your data a name

    A variable is a labelled box for a value. Make one with let. Later you can use the name instead of the value. To mix text and variables, use a template string with backticks ` and ${ }.

    let player = "Mia";
    let level = 3;
    console.log(`${player} is on level ${level}`);   // Mia is on level 3

    Your challenge

    Using the variables name and age (not by typing the words yourself), log Sam is 14 years old.

    💡 Hint

    Use backticks: console.log(`${name} is ${age} years old`);

    👀 Show the solution
    let name = "Sam";
    let age = 14;
    console.log(`${name} is ${age} years old`);
    Console
    Press ▶ Run to see the output.

      Lesson 3 of 9

      Numbers and math

      JavaScript is a calculator: + adds, - subtracts, * multiplies and / divides. Just like in math class, multiplication goes before addition and subtraction. You can change a variable, too: coins = coins - 3;

      let score = 10;
      score = score + 5;
      console.log(score);      // 15
      console.log(2 + 3 * 4);  // 14, not 20

      Your challenge

      You have coins and each potion costs price. Buy 2 potions: take the cost away from coins (using *) and log how many coins are left.

      💡 Hint

      coins = coins - 2 * price; and then console.log(coins);

      👀 Show the solution
      let coins = 7;
      let price = 3;
      coins = coins - 2 * price;
      console.log(coins);
      Console
      Press ▶ Run to see the output.

        Lesson 4 of 9

        Change the web page

        Now the fun part: JavaScript can change the page. document.querySelector("#title") finds the element with id="title". Then .textContent changes its text and .style.color its colour. This is called the DOM: the page as JavaScript sees it.

        let box = document.querySelector("#msg");
        box.textContent = "JavaScript was here";
        box.style.color = "orange";

        Your challenge

        Change the heading #title so it says I can code! and make it teal.

        💡 Hint

        Two lines: document.querySelector("#title").textContent = "I can code!"; and the same with .style.color = "teal";

        👀 Show the solution
        let title = document.querySelector("#title");
        title.textContent = "I can code!";
        title.style.color = "teal";

        🎮 Practise the same idea in the game: JavaScript & the Web, level 2 (free).

        Your page
        Console
        Press ▶ Run to see the output.

          Lesson 5 of 9

          React to a click

          Web pages wait for things to happen: a click, a key, a tap. These are events. With addEventListener("click", …) you tell a button what to do when someone clicks it. The code inside the curly brackets runs on every click, not right away.

          let button = document.querySelector("#btn");
          button.addEventListener("click", function () {
            console.log("Ouch!");
          });

          Your challenge

          When someone clicks the button #btn, the paragraph #out should say Clicked!. Try clicking the button in the preview yourself.

          💡 Hint

          Inside the function, write document.querySelector("#out").textContent = "Clicked!";

          👀 Show the solution
          let button = document.querySelector("#btn");
          button.addEventListener("click", function () {
            document.querySelector("#out").textContent = "Clicked!";
          });

          🎮 Practise the same idea in the game: JavaScript & the Web, level 3 (free).

          Your page
          Console
          Press ▶ Run to see the output.

            Lesson 6 of 9

            Make decisions with if / else

            Programs make choices with if. If the condition in the brackets is true, the first block runs, otherwise the else block runs. Compare with === (equal), >, <, >= and <=. What someone types in a field is text, so turn it into a number with Number( ).

            let score = 120;
            if (score >= 100) {
              console.log("New high score!");
            } else {
              console.log("Try again");
            }

            Your challenge

            When #go is clicked, read the age from #age. If it is 13 or more, show Welcome! in #out, otherwise show Sorry, too young.

            💡 Hint

            Inside the click function: let age = Number(document.querySelector("#age").value); then if (age >= 13) { … } else { … }

            👀 Show the solution
            document.querySelector("#go").addEventListener("click", function () {
              let age = Number(document.querySelector("#age").value);
              let out = document.querySelector("#out");
              if (age >= 13) {
                out.textContent = "Welcome!";
              } else {
                out.textContent = "Sorry, too young.";
              }
            });
            Your page
            Console
            Press ▶ Run to see the output.

              Lesson 7 of 9

              Repeat with a for loop

              Need to do something 5 times? Or 500? A for loop repeats code for you. for (let i = 1; i <= 5; i++) means: start with i at 1, keep going while i is 5 or less, and add 1 after each round. To add something to the page, create an element and append it.

              for (let i = 1; i <= 3; i++) {
                console.log("Round " + i);
              }
              // Round 1, Round 2, Round 3

              Your challenge

              With a for loop, add 5 items to the list #list: Level 1, Level 2 … Level 5.

              💡 Hint

              Inside the loop: let li = document.createElement("li"); li.textContent = "Level " + i; list.appendChild(li);

              👀 Show the solution
              let list = document.querySelector("#list");
              for (let i = 1; i <= 5; i++) {
                let li = document.createElement("li");
                li.textContent = "Level " + i;
                list.appendChild(li);
              }
              Your page
              Console
              Press ▶ Run to see the output.

                Lesson 8 of 9

                Write your own function

                A function is a mini-program with a name. You write it once and use it as often as you like. It can take inputs (parameters) and give back a result with return.

                function greet(name) {
                  return "Hi, " + name + "!";
                }
                console.log(greet("Leo"));   // Hi, Leo!

                Your challenge

                Write a function double(n) that returns the number times two. Then log double(21).

                💡 Hint

                function double(n) { return n * 2; } and then console.log(double(21));

                👀 Show the solution
                function double(n) {
                  return n * 2;
                }
                
                console.log(double(21));
                Console
                Press ▶ Run to see the output.

                  Lesson 9 of 9

                  Final project: a clicker game

                  Time to put it all together: a variable for the score, an event for the click, the DOM to show the score, and an if for a reward. This is how every clicker game starts. When it works, play it in the preview!

                  Your challenge

                  Every click on #click adds 1 to the score and shows it in #score. When the score reaches 10, #msg should say You win!

                  💡 Hint

                  Keep let score = 0; outside the click function. Inside: score = score + 1;, update #score, then if (score === 10) { … }

                  👀 Show the solution
                  let score = 0;
                  let button = document.querySelector("#click");
                  
                  button.addEventListener("click", function () {
                    score = score + 1;
                    document.querySelector("#score").textContent = score;
                    if (score === 10) {
                      document.querySelector("#msg").textContent = "You win!";
                    }
                  });

                  🎮 Practise the same idea in the game: JavaScript & the Web, level 10 (part of the full course).

                  Your page
                  Console
                  Press ▶ Run to see the output.

                    Common JavaScript mistakes beginners make

                    What to learn next

                    Finished all 9 lessons? Nice work. Here is where to go from here:

                    For parents and teachers

                    This page is free and needs no account. Teens can work through it on their own in about an hour. If you want to follow their progress, give them a whole course with stars, hints and a certificate, or run it with a class, HexoQuest for families and HexoQuest for teachers add a dashboard, class codes instead of student emails, and no ads or chat. There is also a guide on how to teach JavaScript to teens.

                    Frequently asked questions

                    Is JavaScript a good first language for teens?

                    Yes. JavaScript runs in every browser, so there is nothing to install, and teens see the result of their code right away on a real web page. It is also one of the most used languages in the world, so what they learn is useful for web pages, games and apps.

                    How long does it take a teenager to learn JavaScript?

                    The basics in this tutorial take about an hour. Building small games and interactive pages takes a few weeks of regular practice, around 20–30 minutes a day. Becoming confident with bigger projects usually takes a few months.

                    Should teens learn JavaScript or Python first?

                    Both are great. JavaScript is the better pick if your teen wants to build websites, browser games or things they can share with a link. Python is often easier to read and is great for data, automation and AI. Many teens learn one and then the other.

                    Do I need to install anything?

                    No. This tutorial and the HexoQuest JavaScript course run completely in the browser on a computer or a Chromebook. A keyboard helps, because you will type real code.

                    What age is this JavaScript tutorial for?

                    It is written for teens aged 13 to 17 who have never coded before. Younger kids who already know some coding can follow it too, ideally with a parent nearby.

                    Is this JavaScript tutorial free?

                    Yes. All lessons on this page are free and need no account. The full JavaScript & the Web course on HexoQuest has its first levels free as well.

                    More guides

                    Ready for the game?

                    The first 3 levels of JavaScript & the Web are free. Build a real browser game and download it at the end.

                    Play level 1 See pricing