Variable
Creating a variable will require you to assign it a name and give it a value using the assignment operator, usually using the equal (=) symbol. Once you have created it, you can use this group of code in your program, update its value, or perform calculations. However, certain naming rules are required to work.
Depending on the coding languages, they have different sets of rules. For example, JavaScript and Python don’t need to declare the type explicitly, unlike Java and C++. Most programming languages use a letter or an underscore for the name. It also does not contain spaces or special characters.
How is a variable used in coding?
In programming languages, there are two basic steps in using variables: declaration and initialisation. The meaning of a declaration is when you are creating a variable and defining its name. Some languages specify it as a data type. This part is important because it tells the computer to make space in memory for that group of code.
On the other hand, initialisation means assigning an initial value to the variable, like giving it a starting value. You can do it along with the declaration or in a separate step. People are using this method because it is simpler to apply. It uses a value from the start, helping in preventing errors that can happen when trying to use the group code that hasn’t had an assigned value yet.
Types of data
A variable can hold different types of data. These types are going to need labels to show their value and what operations you can do on them. Depending on the programming language, most of them use a common set of basic (primitive) data types and many support complex (advanced) ones. For instance, here are the common types:
- Integer: Whole numbers (e.g., 10, -5, or 2)
- Float or Double: Decimal numbers (e.g., 0.3, 3.14, or -0.04)
- String: Text data (e.g., “word” or “123”)
- Boolean: True or False values (“True”/”False”)
- Character: A single letter or symbol (e.g., ‘A’ or ‘$’)
- Array or List: A collection of items (e.g., [1, 2, 3] or [A, B, C]
- Dictionary or Map or Object: Key-value pairs (e.g., {“name”: “Michael”}
- Null or None: It represents no value or absence of data
Scope
The way programming languages work is that there is a limited area in a program where a variable is accessible or visible. Therefore, the scope defines where you can use a particular code in your code. It is helpful to prevent name conflicts and control which parts can be seen and modified. Overall, it helps to manage and makes programs easier to understand. There are three main types of scope, which are:
- Global scope: It is a variable you can use outside of any function or block. What it means is that you can access and use it anywhere in the program after its declaration.
- Local scope: In this one, the code only works within the designated function or block.
- Block scope: Not every language has this type of scope. In this one, the variable defined inside loops or conditional blocks (like if or for) is only accessible within those blocks. You can find this type in JavaScript.
Naming conventions
When you are writing in English, there are grammar rules, standards, and styles for writing a sentence, so it is easy for people who read it to understand. Similar to coding languages, they have guidelines and styles for writing names of variables, functions, classes, and other elements. Naming conventions can help make code readable, consistent, and maintainable. Here are the common naming styles:
- camelCase: This is where the first word is in lowercase and the following words are capitalised, like userName or calculateTotal. You can find this type in JavaScript and Java.
- PascalCase: Every word starts with a capital letter. People use this variable for class names and constructors, such as UserProfile or PaymentProcessor.
- Snake_case: In this type, all the words are in lowercase, and each of them is separated by underscores, such as user_name or calculate_total. You can find this kind of Python for variables and functions.
- SCREAMING_SNAKE_CASE: Similar to snake_case but all in uppercase. People use this for conventions, like MAX_SPEED or DEFAULT_TIMEOUT.
Mutability and constants
Mutability means the value of a variable can be changed after it has been created. The way it works is that you can change the content of a mutable object without creating a new one. On the other hand, an immutable object cannot be changed once it’s created. If you tried to, it just creates a new object instead.
For a constant, it is a variable whose value cannot be changed after it is assigned. People use this to store fixed values that should stay the same throughout the program. One example is mathematical constants (PI=3.14) or configuration settings.
Answer: Variable hoisting in JavaScript helps in moving declarations to the top of their scope, allowing them to be used before they are declared.
Answer: Variable shadowing occurs when a local variable has the same name as a variable in an outer scope, hiding the outer one.
Answer: Mutable variables can have their values changed after creation, while immutable variables cannot be altered once assigned.





