Digital Tech Notes (JSS1 - SS3)

Variables In Programming

Learn what variables in programming mean, how they are used, and how to create and name variables in Scratch. A complete JSS 2 Computer Studies guide.



INTRODUCTION

Think about the last time you played a video game. Every time you collected a coin or defeated an enemy, your score went up. That number on your screen — your score — had to be stored somewhere inside the game. The game needed a way to remember your progress from one second to the next. That "somewhere" is what programmers call a variable.

Variables are one of the most fundamental ideas in programming. Without them, computers would be unable to remember anything during the running of a program. No scores, no names, no ages, nothing. Every useful program you have ever seen — from the apps on your phone to the attendance register software your school might use — depends on variables to function properly.

For JSS 2 students in Nigeria, understanding variables is a big step forward in your Computer Studies journey. The Nigerian NERDC curriculum introduces programming concepts at this level specifically because the world is moving fast into a digital future. Whether you want to become a software developer, a game designer, or simply a tech-savvy person, knowing how variables work will always be useful to you.

In this lesson, we will explore what variables are, why they matter, how they work, and how you can create and use them in Scratch — one of the most beginner-friendly programming environments in the world.


LEARNING OBJECTIVES

By the end of this lesson, students should be able to:

  1. Define the term "variable" as used in programming
  2. Explain the importance and uses of variables in a computer program
  3. Identify different types of data that can be stored in variables
  4. Describe the rules for naming variables correctly
  5. Demonstrate how to create and use variables in Scratch
  6. Apply variables to solve simple real-life problems using Scratch

WHAT IS A VARIABLE IN PROGRAMMING?

A variable is a named storage location in a computer's memory that holds a value which can change as the program runs. Think of it like a container or a box with a label on it. The label is the name of the variable, and whatever is inside the box is the value.

For example, imagine a box labeled "StudentAge." Inside the box, you put the number 14. Later in the program, when the student has a birthday, you can open that same box and replace 14 with 15. The label (StudentAge) stays the same, but the value inside has changed. That is exactly how a variable works.

In everyday Nigerian life, you can compare a variable to a market price tag. The price of garri or rice at the market does not stay the same forever — it changes from week to week. But the label "price of garri" always means the same thing, even when the actual amount changes. The label is your variable name; the current price is your variable value.

THE KEY PARTS OF A VARIABLE

Every variable has three important parts:

Name — This is the label you give to the variable so you can find and use it later. For example: score, playerName, age.

Value — This is the actual information stored inside the variable. For example: 50, "Chioma", 14.

Data Type — This describes what kind of information the variable holds. A variable can hold a number, a word or sentence (called a string), or a true/false answer (called a boolean).

Understanding these three parts will make it much easier for you to work with variables in any programming language, including Scratch.


WHY ARE VARIABLES IMPORTANT IN PROGRAMMING?

Variables are important because they make programs flexible, powerful, and dynamic. Without variables, a program can only do one fixed thing. With variables, a program can respond to different users, different inputs, and different situations.

Here are some of the main reasons why variables matter:

Storing User Input — When you type your name into an app on your phone, the app uses a variable to temporarily store that name so it can display it or use it later. If you use the MTN or Airtel self-care app and it greets you with your own name, that is a variable at work.

Keeping Score — In games like a quiz app or a football simulation game, variables store the current score. Every time a player answers correctly or scores a goal, the score variable increases by a certain number.

Counting and Tracking — A variable can count how many times something has happened. For example, a variable called "attempts" can track how many times a student has tried to answer a question.

Making Decisions — Programs use variables to make decisions. For example, if a student's score variable is above 50, the program can display "You passed." If it is below 50, it displays "Try again."

Personalizing Programs — Variables allow programs to behave differently for different users. When you log in to your school portal or a learning app, your username and personal details are retrieved and stored in variables so the app can serve you specifically.


TYPES OF VARIABLES (BASED ON DATA TYPE)

Different kinds of information can be stored in variables. Here are the most common types:

Numeric Variables — These store numbers. Examples: age = 13, score = 75, numberOfStudents = 40. You can use these for calculations like addition, subtraction, multiplication, and division.

String Variables — These store text or words. In programming, text is called a "string." Examples: studentName = "Emeka", schoolName = "Government Secondary School", subject = "Computer Studies."

Boolean Variables — These store only two possible values: true or false. They are used when a program needs to check a condition. Example: isPassed = true, isLoggedIn = false.

In Scratch, you will mostly work with numeric and string variables, which makes it easy to get started.


RULES FOR NAMING VARIABLES

Naming variables correctly is very important in programming. If you break the naming rules, your program will have errors and will not work. Here are the standard rules every programmer must follow:

A variable name must not contain spaces — Instead of "student name," write studentName or student_name.

A variable name should be meaningful — Instead of calling a variable "x" or "abc," call it something that describes what it stores, like totalScore or playerHealth. This makes your code easier to understand.

A variable name must not start with a number — "2score" is wrong. "score2" is acceptable.

A variable name should not be a reserved word — Words like "if," "while," "for," and "true" have special meanings in programming and cannot be used as variable names.

In Scratch, these rules are relaxed a little because Scratch allows you to name variables with spaces and plain English phrases. However, it is a good habit to keep names short and descriptive.

GOOD VARIABLE NAMES VS BAD VARIABLE NAMES

Good: playerScore, studentAge, totalMarks, itemPrice

Bad: x, abc123, my variable (with space in most languages), 1stScore (starts with number)

The habit of giving clear, meaningful names to variables is something professional developers take seriously. It is called "clean code," and it makes a huge difference when you or someone else reads your program later.


INTRODUCTION TO SCRATCH

Scratch is a free, visual programming language developed at MIT (Massachusetts Institute of Technology). It was designed specifically for beginners, especially young learners aged 8 to 16. Instead of typing lines of code, you drag and drop colorful blocks to build programs. This makes it perfect for JSS students who are just starting to learn programming.

Scratch is used in schools across Nigeria and around the world as a first programming environment. You can create games, animations, interactive stories, and educational tools using Scratch. The website is scratch.mit.edu and it works on most computer browsers.



HOW TO CREATE VARIABLES IN SCRATCH



Creating a variable in Scratch is straightforward. Follow these steps carefully:

Step 1 — Open Scratch by visiting scratch.mit.edu on your computer or school lab browser. You can also use the offline Scratch editor if your school has it installed.

Step 2 — Start a new project or open an existing one.

Step 3 — On the left side of the screen, you will see a list of block categories. Look for the category called "Variables." It is usually orange in color. Click on it.

Step 4 — At the top of the Variables panel, you will see a button that says "Make a Variable." Click on it.

Step 5 — A small dialog box will appear asking you to type a name for your variable. Type a meaningful name, such as "score" or "playerName" or "totalMarks."

Step 6 — You will be asked to choose whether the variable is "For all sprites" or "For this sprite only." For most beginner projects, choose "For all sprites" so the variable can be used by the whole program.

Step 7 — Click OK. Your new variable will now appear in the Variables panel as an orange block. It will also appear as a small display box on the Stage (the area where your animations play).

Once your variable is created, Scratch gives you several blocks to work with it:

Set [variable] to [value] — This block assigns a specific value to your variable. For example, "set score to 0" resets the score to zero at the start of a game.

Change [variable] by [number] — This block adds or subtracts from the current value. For example, "change score by 10" adds 10 to whatever the score already is.

Show variable / Hide variable — These blocks let you control whether the variable's value is visible on the screen during the game.


PRACTICAL EXAMPLE: BUILDING A SIMPLE QUIZ SCORE TRACKER IN SCRATCH

Here is a simple activity you can try in your school's computer lab or at home if you have access to Scratch:

Imagine you are making a simple quiz game in Scratch. A character appears on the screen and asks the player a question. If the player answers correctly, they get 10 points. If they answer wrongly, they get no points.

To build this, you would create a variable called "score" and set it to 0 at the start. Each time the player answers correctly, you use the "change score by 10" block. At the end of the quiz, the sprite can announce the total score using the variable.

This is exactly how real quiz apps and educational platforms work. Apps like Kahoot or the quiz features on some Nigerian e-learning platforms use variables in exactly this way — just in more advanced programming languages like JavaScript or Python.


PRACTICAL APPLICATIONS OF VARIABLES IN REAL LIFE (NIGERIAN CONTEXT)

Variables are not just classroom theory. They are everywhere in the digital tools Nigerians use daily:

Bank Account Balance — When you use a GTBank, Access Bank, or Opay mobile app, your account balance is stored in a variable on the bank's system. Every time money enters or leaves your account, that variable is updated.

WAEC and NECO Score Systems — When results are computed, each student's scores in different subjects are stored in variables. The system then calculates totals and grades using those variable values.

Fuel Price Tracking — Apps and websites that monitor fuel prices across Nigerian states use variables to store and update the current price in different locations.

Data Bundle Tracking — When you buy data on your MTN, Airtel, Glo, or 9mobile line, the remaining data balance is a variable. Each time you use the internet, the system reduces that variable by the amount consumed.

These examples show that variables are not abstract concepts — they are the backbone of every digital system around you.


ADVANTAGES OF USING VARIABLES IN PROGRAMMING

Variables make it possible to write flexible programs that work differently depending on the input or situation.

They allow programs to store and reuse information without asking the user for it repeatedly.

Variables make programs easier to update. If a value changes, you only need to change it in the variable — not in every single place it appears in the code.

They enable programs to perform calculations, comparisons, and logical decisions.

Variables make code easier to read and understand when given proper names.

DISADVANTAGES / LIMITATIONS

If a variable is given a wrong or confusing name, the code becomes very difficult to understand or debug.

Using too many variables without proper organization can make a program messy and hard to manage.

If a variable is assigned the wrong data type (for example, storing a word where a number is expected), it can cause errors in the program.

Variables stored in a computer's memory are temporary — once the program closes, the values are usually lost unless saved to a file or database.


ETHICAL AND SAFETY CONSIDERATIONS

As you learn to use variables in programming, especially for storing user data, it is important to think about responsibility:

Do not store other people's personal information (like names, phone numbers, or passwords) in a program unless you have permission and a good reason.

Be careful about what kind of data your programs collect. Programs that store sensitive information have a responsibility to protect that data.

In professional programming, developers must comply with data protection laws. Nigeria has a data protection regulation (the Nigeria Data Protection Regulation, NDPR) that governs how personal information should be handled.

Always think about what happens to the data stored in your variables and whether it could be misused.


CLASSROOM AND HOME ACTIVITIES

Activity 1 — Variable Hunt: Look around your school or home and identify five real-life examples of things that change over time (like a water level in a tank, your score in a game, or the number of people in a room). For each one, write a suitable variable name and a possible current value.

Activity 2 — Scratch Score Game: Open Scratch and create a simple project with one sprite. Create a variable called "score." Use blocks to make the score increase by 5 every time the sprite is clicked. Add a "set score to 0" block at the start so the score resets each time the project is run.

Activity 3 — Name the Variable: Your teacher will call out different situations (e.g., "a variable to store a student's first name," "a variable to count the number of absent students"). Students must quickly suggest a good variable name for each situation.

Activity 4 — Debug the Name: Write these variable names on the board and ask students to identify which ones are correct and which ones break the rules: 1score, totalScore, student age, numberOfBooks, for, myAge2. Students should explain why each is correct or incorrect.


ASSESSMENT QUESTIONS

Objective Questions:

  1. What is a variable in programming? a) A fixed value that never changes b) A named storage location that holds a value which can change c) A type of computer hardware d) A programming language

  2. Which of the following is a valid variable name? a) 1stScore b) my score c) totalMarks d) for

  3. In Scratch, which block is used to give a variable a starting value? a) Change variable by b) Show variable c) Set variable to d) Hide variable

  4. Which type of variable stores text or words? a) Boolean b) Integer c) String d) Float

  5. What happens when you click "Make a Variable" in Scratch? a) A new sprite is created b) A dialog box appears asking for the variable name c) The program runs automatically d) The stage is cleared

Theory Questions:

  1. Define the term "variable" as used in computer programming and explain its three key parts with examples.

  2. Describe the steps involved in creating a variable in Scratch. Include what each step involves and what happens after the variable is created.

  3. Give three real-life examples from a Nigerian context where variables are used in digital systems. For each example, state the variable name you would use and the type of data it would store.


SUMMARY

In this lesson, we learned that a variable is a named storage location in a computer's memory that holds a value which can change during the running of a program. Every variable has a name, a value, and a data type. Variables can store numbers, text (strings), or true/false values (booleans). Proper naming of variables is essential — names should be meaningful, should not contain spaces (in most languages), and should not start with numbers or use reserved words. In Scratch, variables are created using the "Make a Variable" button in the Variables section, and can be manipulated using blocks like "set to" and "change by." Variables are used everywhere in real life — from bank apps to school result systems — making them one of the most important concepts in all of programming.


CONCLUSION

Variables are truly the memory of a program. Just as your brain stores your name, your friends' names, and what you ate for breakfast this morning, a program uses variables to remember the information it needs in order to function. As a JSS 2 student, you are now at a stage where you are beginning to think like a programmer. Understanding variables is one of the most important milestones on that journey.

The world of technology is growing rapidly in Nigeria. From Lagos to Kano, Enugu to Port Harcourt, young Nigerians are building apps, creating games, and developing software that is changing lives. The concepts you are learning today — including variables — are the same ones that professional developers use every single day.

Keep practicing in Scratch, ask questions, and do not be afraid to make mistakes. Every error is a lesson, and every lesson brings you closer to becoming a confident, skilled programmer.


FREQUENTLY ASKED QUESTIONS (FAQ)

What is the difference between a variable and a constant in programming? A variable is a storage location whose value can change while the program is running. A constant, on the other hand, holds a value that is set once and never changes. For example, the value of pi (3.14159) is usually stored as a constant because it never changes, while a student's score is stored as a variable because it changes as the student answers more questions.

Can a variable hold more than one value at a time? No, a basic variable can only hold one value at a time. If you assign a new value to a variable, the old value is replaced. However, there are more advanced data structures called arrays or lists that can hold multiple values — but these are topics you will study at a higher level.

Why is Scratch a good environment for learning variables? Scratch uses a visual, drag-and-drop interface that removes the difficulty of typing code. This makes it much easier for beginners to focus on understanding concepts like variables without worrying about spelling mistakes or syntax errors. It is widely used in Nigerian schools and around the world for this reason.

Do variables in Scratch work the same way as in other programming languages? The concept is the same — a variable stores a value that can change. However, the way you create and use variables looks different in text-based languages like Python or JavaScript, where you type commands instead of using blocks. The logic, though, is identical.

What happens to the value of a variable when a Scratch project is stopped? When a Scratch project is stopped and restarted, variables usually reset to whatever value you set them to at the beginning of the program (using the "set to" block). If you do not include a "set to" block at the start, the variable might retain its last value, which can cause unexpected behavior in your program.

Is programming with variables relevant to the WAEC Computer Studies examination? Yes. The WAEC and NECO syllabuses for Computer Studies include topics on programming concepts, and variables are a core part of that. Understanding variables will help you answer both theory and practical questions related to programming in your examinations.

I am Echofu George Adah, the Pioneer of Digital Tech Note and the Founder and Chief Executive Officer of GeoWeb Technologies Limited, an information technology company committed to providing cutting-edge ICT solutions.