Understanding Variables
Variables are fundamental building blocks in programming. They act as containers for storing data values that your program can manipulate and reference throughout its execution.
What is a Variable?
Imagine a variable as a labeled box. You can put a value inside the box, and later, you can retrieve that value or replace it with something else. The label on the box is the variable's name, which you use to refer to its contents.
Declaring and Initializing Variables
Before you can use a variable, you typically need to declare it. Declaration tells the program that a variable with a specific name exists. Often, you will also initialize the variable by assigning it an initial value at the time of declaration.
Syntax
The exact syntax for declaring and initializing variables varies slightly depending on the programming language, but the concept is the same. Here are common patterns:
// Declare a variable named 'userAge' and assign it the value 30
DECLARE userAge = 30
// Declare a variable named 'userName' and assign it the string "Alice"
DECLARE userName = "Alice"
// Declare a variable named 'isLoggedIn' and assign it the boolean value true
DECLARE isLoggedIn = true
var
, let
, or const
for declaration.
Variable Naming Rules
To keep your code organized and readable, it's important to follow consistent naming conventions for your variables:
- Variable names should be descriptive, indicating the purpose of the data they hold.
- They typically start with a letter or an underscore (
_
). - They can contain letters, numbers, and underscores.
- They are case-sensitive (e.g.,
myVariable
is different frommyvariable
). - Avoid using reserved keywords (words that have special meaning in the programming language) as variable names.
firstName
) or snake_case (e.g., first_name
) for multi-word variable names, depending on your language's convention.
Assigning and Reassigning Values
Once a variable is declared, you can assign a value to it. You can also change (reassign) the value stored in a variable later in your program.
// Declare a variable
let score = 0;
console.log("Initial score:", score); // Output: Initial score: 0
// Assign a new value
score = 100;
console.log("Updated score:", score); // Output: Updated score: 100
// Reassign with a different type (if allowed by language)
score = "High Score";
console.log("Score after type change:", score); // Output: Score after type change: High Score
Types of Variables
While the fundamental concept of a variable remains the same, languages often differentiate between types of variables based on their mutability (whether their value can be changed after initialization):
- Mutable Variables: Their values can be changed after they are created. Most variables fall into this category by default.
- Immutable Variables: Their values cannot be changed once they are assigned. These are useful for constants or values that should never be altered.
Key Takeaways
- Variables store data.
- They have a name and a value.
- You declare and initialize variables.
- Follow naming conventions for readability.
- Variable values can often be reassigned.