0Pricing
Javascript for Kids · Lesson

Heads or Tails

Random coin flip.

Heads or Tails is a free Javascript for Kids lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Javascript for Kids learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Welcome to Coin Flip! 🪙

Hi there, coder! 👋 Today we are going to build a Coin Flip Game! 🪙

When you flip a real coin, it lands on Heads or Tails. We never know which one will come up. That is what makes it fun! 🎉

Let's teach the computer to flip a coin too!

What is Random? 🎲

A coin flip is random. Random means we cannot guess the answer for sure. 🤷

JavaScript has a special helper called Math.random(). It gives us a surprise number between 0 and 1. 🎲

Let's see what it gives us!

console.log(Math.random());
console.log(Math.random());
console.log(Math.random());

Tiny Numbers 🔢

Did you see those numbers? They were tiny and full of decimals, like 0.3829. 😮

Every time you run it, you get a different number. The computer is rolling an invisible dice for us! 🎲

let surprise = Math.random();
console.log('My surprise number is: ' + surprise);

Less Than Half? ✂️

A coin has 2 sides. So we need to split our number into 2 groups! ✂️

The middle of 0 and 1 is 0.5 (one half). We can ask: is our number less than 0.5? 🤔

let number = Math.random();
console.log('Number: ' + number);
console.log('Is it less than 0.5? ' + (number < 0.5));

Heads or Tails Time! 🎯

Now the magic! If the number is less than 0.5, we say Heads. If not, we say Tails! 🎯

We use if and else to make the choice.

let number = Math.random();
if (number < 0.5) {
  console.log('Heads! 🙂');
} else {
  console.log('Tails! 🪙');
}

Run It Again! 🔁

Try clicking Run a few times! 🔁

Sometimes you get Heads, sometimes you get Tails. Just like a real coin! 🪙

The computer surprises us every single time. 😄

let number = Math.random();
if (number < 0.5) {
  console.log('Heads! 🙂');
} else {
  console.log('Tails! 🪙');
}

Saving the Result 📦

Let's keep the answer in a box called a variable. 📦

We name it result and put either 'Heads' or 'Tails' inside it.

let number = Math.random();
let result;
if (number < 0.5) {
  result = 'Heads';
} else {
  result = 'Tails';
}
console.log('The coin shows: ' + result);

A Flip Function 🛠️

We will flip many times in our game. So let's make a function we can reuse! 🛠️

A function is like a little machine. We call flipCoin() and it gives back a result! 🤖

function flipCoin() {
  if (Math.random() < 0.5) {
    return 'Heads';
  } else {
    return 'Tails';
  }
}
console.log(flipCoin());

Use the Machine 🤖

Now that we have our coin machine, we can use it again and again! 🤖

Watch how we call flipCoin() three times in a row! 🪙🪙🪙

function flipCoin() {
  if (Math.random() < 0.5) {
    return 'Heads';
  } else {
    return 'Tails';
  }
}
console.log('Flip 1: ' + flipCoin());
console.log('Flip 2: ' + flipCoin());
console.log('Flip 3: ' + flipCoin());

Make it Fancy ✨

Let's add fun emojis to show the side of the coin! ✨

We add a happy message to make our game more exciting. 🎉

function flipCoin() {
  if (Math.random() < 0.5) {
    return 'Heads 🙂';
  } else {
    return 'Tails 🪙';
  }
}
console.log('Flipping the coin... 🌀');
console.log('It landed on: ' + flipCoin());

You Made a Coin! 🏆

Amazing work! 🏆 You taught the computer to flip a coin using Math.random()!

  • 🎲 Random gives a surprise number
  • ✂️ We split it at 0.5
  • 🪙 Less than 0.5 = Heads, else Tails

Next, we will let a player guess the coin! 🤔

Quick Check ✅

Let's test your brain! 🧠 What does Math.random() give us?

Lesson Recap 🎀

Great job today! 🎀 Here is what you learned:

  • 🎲 Math.random() makes a random number
  • ✂️ We compare it with 0.5
  • 🪙 We pick Heads or Tails with if/else
  • 🛠️ A flipCoin() function lets us reuse our code

See you in the next lesson, coin master! 👋

Frequently asked questions

Is the “Heads or Tails” lesson free?

Yes — the full text of “Heads or Tails” is free to read here on the web, and the Javascript for Kids course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Javascript for Kids course, upgrade to CoddyKit PRO.

What will I learn in “Heads or Tails”?

Random coin flip. You practise Javascript for Kids with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Javascript for Kids?

No prior experience is required. Javascript for Kids on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Heads or Tails” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Javascript for Kids lesson?

Yes. Every Javascript for Kids lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Heads or Tails
  2. Guessing
  3. Checking the Guess
  4. Flip Many Times
← Back to Javascript for Kids