DEV Community

Cover image for ## Blockchain Explained Like You’re 5 (With Code)
Amir of Ekiti
Amir of Ekiti

Posted on

## Blockchain Explained Like You’re 5 (With Code)

Blockchain Explained Like You’re 5 (With Code)

Imagine you and your friends are playing a game of marbles at recess. You all trust each other (kind of), but there's no teacher around to keep score.

So you come up with a plan:

Every time someone wins or loses marbles, you all write it down in your notebooks.

That’s the idea behind a blockchain.

Let me explain.


🧠 What Is a Blockchain (Like You’re 5)

You and your friends:

  • Agree on the rules: If you lose, you give marbles.
  • Write down every trade in your own notebook.
  • Keep all your notebooks in sync.
  • If someone tries to cheat and says “I didn’t lose 3 marbles,” the others can say, “Look! It’s in all our notebooks!”

That notebook?

That’s a blockchain — a shared list of events that everyone agrees on and no one can secretly change.


🧱 What’s a Block?

You don’t write down every marble trade immediately. Instead, every 10 trades, you:

  • Wrap them up
  • Add a date and summary
  • Put it in your notebook and call it a block

Each block is like a chapter in your notebook, and it has:

  • What happened
  • When it happened
  • A fingerprint of the previous block (so no one can remove or change a chapter)

So it’s a chain of blocks. Now we’re talking blockchain.


🧪 Let's Code a Baby Blockchain (in JavaScript)

Let’s build a tiny blockchain to understand how it works.

const crypto = require('crypto');

class Block {
  constructor(index, timestamp, data, previousHash = '') {
    this.index = index;
    this.timestamp = timestamp;
    this.data = data;
    this.previousHash = previousHash;
    this.hash = this.calculateHash();
  }

  calculateHash() {
    return crypto
      .createHash('sha256')
      .update(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data))
      .digest('hex');
  }
}

class Blockchain {
  constructor() {
    this.chain = [this.createGenesisBlock()];
  }

  createGenesisBlock() {
    return new Block(0, Date.now(), "Genesis Block", "0");
  }

  getLatestBlock() {
    return this.chain[this.chain.length - 1];
  }

  addBlock(newBlock) {
    newBlock.previousHash = this.getLatestBlock().hash;
    newBlock.hash = newBlock.calculateHash();
    this.chain.push(newBlock);
  }

  isChainValid() {
    for (let i = 1; i < this.chain.length; i++) {
      const current = this.chain[i];
      const previous = this.chain[i - 1];

      if (current.hash !== current.calculateHash()) return false;
      if (current.previousHash !== previous.hash) return false;
Enter fullscreen mode Exit fullscreen mode

✅ Each block is linked to the one before

✅ Every change makes a new hash

✅ Tamper-proof unless someone rewrites the whole chain (and gets everyone else to agree!)


🧊 But What About Crypto?

Blockchains like Bitcoin or Ethereum use this structure to track:

  • Who sent money to whom
  • Who owns what
  • And make sure it’s impossible to cheat (without a lot of power and math)

Instead of marbles, they’re trading coins or tokens.


👩‍🚀 Why Should You Care?

Because this idea:

  • Makes money programmable
  • Can create transparent systems (like for voting or public spending)
  • Helps us build apps where no one can lie about the history

And you just built one (a tiny one, but still 😄).


🧠 TL;DR

  • A block is a bundle of data + timestamp + a hash.
  • A blockchain is a secure, connected list of those blocks.
  • It’s like a shared notebook where everyone writes the truth—and if someone tries to lie, it breaks the chain.

💬 Got Questions?

Let me know in the comments!

Want a follow-up where we deploy this to a real blockchain? Or build a smart contract version?

Hit the ❤️ if this helped you understand blockchain just a bit better.


👋 Let’s Connect

Top comments (0)