In summary, variables are like containers that are used to store data or information in programming languages like Scratch or any other programming language.
We can save distinct values using variables. It could be a name, a number, or any other kind of data.
Table of contents
Types of Data
- String: which can be written inside double quotes e.g.; “Varun Singh”, “Brightchamps is the best place for learning coding for kids”
- Integer: whole numbers without decimal part is called integer e.g., 12,5,8
- Float: Numbers with decimal parts. E.g., 23.05, 52.33, 12.1
- Boolean: True or false Boolean values
Here “Name” is the variable that can store any values like John or Chris. Similarly, the variable “Place” can store different places
Variable Declaration
Using the keyword ‘var’ we can declare a variable. Assume we need to store data; we can do so using the following method.
var a = “Arun Singh”; var b = 36; var v = true;
Scope of Variables
Variables in a program have a scope and they live and die within the scope.
A variable declared inside a function or any block of code starting and ending with { } (for example – for loop) have scope defined only inside the function (or curly brackets).
Any other function or block of code cannot directly access it. They are referred to as having a local scope.
Global scope variables are those that are declared outside of the top-level functions. You can therefore access them from wherever in the code.
As an example, if you put on a headphone and listen to music, can your friend around you hear the music?
The answer would be no.
On the other hand, everyone can hear the music played at the party by the speaker, right?
Here, Headphones are a local source of music, while speakers are a global music source for everyone.
In short, headphones are like a local variable, which means that only the corresponding listener with the headphone can access the music, whereas the speaker is like a global variable, which means that anyone can access it.
Similarly, in programming, variables declared outside the function at the top are said to have global scope because any function can access the variable.
On the other hand, the variables which are declared inside a particular function have a local scope because only that particular function can have access to that variable
Types of Variables in Scratch
In Scratch, there are three types of variables. In addition to the Global and Local variables, there is also a cloud variable.
Let us briefly discuss all three of them;
- Global variable: Global variable is the standard variable that can be changed or accessed from any sprite in the project or stage. It doesn’t matter which sprite it was created on.
- Local variable: A local variable can only be changed or accessed by the sprite that created it. This variable is not accessible to any other sprite and can be considered the stark opposite of the Global Variable.
- Cloud variables: Cloud variables, distinguished by a cloud-like symbol in front of the variable name, allow users to store variables on the scratch server. Cloud variables are extremely fast to update.
The cloud variable only accepts numeric data and has a maximum data size of 256 characters as of Scratch 3.0. You can create up to ten cloud variables in a single Scratch project.
Conclusion
The use of variables is essential in computer programming. Variables function as data containers by storing changeable values in memory.
They are designed to function so that they may only hold one value at a time, and that value may be anything from strings to booleans to numbers.
Scratch, with its simple block-based programming language, is an excellent platform for learning the concept of variables.
As a user creates projects in Scratch, they will better understand the function and application of variables.
Learn Scratch at BrightChamps with its specially designed curriculum that makes learning programming easy for students in Grade 1-12.
BrightChamps also teaches a variety of other programs that help kids build a foundation in computer programming through activities, interactive lessons, and other means.
BrightChamps provides financial education for kids, equipping them with essential money management knowledge for a successful future.