To create a variable, you give it a name and an initial value, like starting a labeled box with something inside it. To update a variable, you reuse that same name and assign it a new value based on an expression, often using its current value, for example myVar=myVar+1myVar=myVar+1myVar=myVar+1.

Creating a variable (in plain language)

When you “create” a variable in code, you are doing three things.

  1. Choose a name
    • This name should describe what the value represents, like score, clickCount, or myVar.
  1. Set an initial value
    • You decide what it starts as, for example:
      • var myVar = 0;
      • var score = 10;
    • This is like saying: “Make a new box called myVar , and put 0 in it.”
  1. Store it for later use
    • After this, you can read that value, show it on screen, or change it as your program runs.

Story-style example:
Imagine you’re running a small game. At the start, you say, “The player’s score is 0.” You’ve just created a score variable and set its starting value.

Updating a variable

Updating a variable means changing what’s inside that “box” while your program runs.

Common ways to update are:

  • Replace with a new value directly
    • myVar = 5;
    • You don’t care what it was before; you just overwrite it.
  • Update based on its old value
    • myVar = myVar + 1;
    • Read the current value of myVar, add 1, then store the result back into myVar.
* This pattern is very common for counters and scores.
  • Other operations
    • You can subtract, multiply, etc.:
      • myVar = myVar - 2;
      • myVar = myVar * 3;

Mini example:

  • Start: var clicks = 0;
  • After 1 update: clicks = clicks + 1; → 1
  • After 2nd update: clicks = clicks + 1; → 2
    Each time you update, you’re using the previous value to compute the new one.

How the Counter Pattern with Event works

The Counter Pattern with Event is a common way to count how many times something happens, like button clicks. It combines:

  • a counter variable, and
  • an event (like a "click" on a button).

In many Code.org–style examples, it looks like this in text code:

javascript

var myVar = 0;

onEvent("id", "click", function() {
  myVar = myVar + 1;
});

Here’s what’s going on step by step:

  1. Initialize the counter
    • var myVar = 0;
    • Create a variable called myVar and start it at 0.
  2. Set up the event listener
    • onEvent("id", "click", function() { ... });
    • This tells the program: “Whenever the thing with ID "id" is clicked, run this function.”
  1. Update the variable when the event happens
    • Inside the function: myVar = myVar + 1;
    • Each click:
      • Read the current value of myVar.
      • Add 1.
      • Store the new value back into myVar.
  1. Result: a running count of events
    • First click: myVar goes from 0 → 1
    • Second click: 1 → 2
    • Third click: 2 → 3, and so on.

Quick story example:
Think of a turnstile at a stadium. You start a counter at 0. Every time someone passes through and triggers the sensor (the event), the controller increments the counter by 1. At any moment, the counter tells you how many people have passed through. This is exactly what the Counter Pattern with Event does in code.

Where this pattern shows up

Some typical uses of the Counter Pattern with Event include:

  • Counting how many times a button is clicked (like a “Like” button).
  • Keeping score in a game each time the player earns points.
  • Tracking how many items a user has added to a cart.

No matter the situation, the idea is the same: start with a variable, then update it every time a specific event occurs.

TL;DR:

  • Creating a variable = naming it and giving it a starting value.
  • Updating a variable = assigning a new value, often using its current value (like myVar = myVar + 1).
  • Counter Pattern with Event = start a counter at 0, then each time an event (like a click) happens, increase the counter with counter = counter + 1.

Information gathered from public forums or data available on the internet and portrayed here.