Every program needs a way to store information while it runs. Whether it's a user's name, a product price, a login status, or the result of a calculation, that information needs a temporary place to live. In programming, that place is called a variable.
A variable is simply a named container that stores a value. Instead of writing the same value repeatedly throughout your code, you store it in a variable and use its name whenever you need it.
Think of a variable as a labeled storage box.
| Variable | Value |
|---|---|
| name | "Sandeep" |
| age | 28 |
| isLoggedIn | true |
Whenever your program needs the user's name or age, it simply looks inside the corresponding box.
Learning Objectives
By the end of this lesson, you'll be able to:
- Understand what a variable is.
- Create variables in both CFScript and Tag Syntax.
- Follow ColdFusion variable naming rules.
- Update variable values.
- Display variables on a web page.
- Use variables in real-world examples.
What Is a Variable?
A variable is a named location in memory that stores data while your application is running. For example:
- A customer's name
- A product price
- A login status
- A total amount
- A message
Instead of hardcoding values, you store them in variables.
Creating Variables in CFScript
CFScript uses the assignment operator (=) to create variables.
<cfscript>
name = "Sandeep";
age = 28;
city = "Faridabad";
</cfscript>Here:
- name stores "Sandeep"
- age stores 28
- city stores "Faridabad"
Displaying Variables
Use writeOutput().
Creating Variables Using Tag Syntax
Both styles produce the same result.
Variables Can Store Different Types of Data
<cfscript>
studentName = "Rahul";
marks = 92;
isPassed = true;
salary = 55000.75;
</cfscript>Variables can hold:
- Text
- Numbers
- Decimal values
- Boolean values
- Arrays
- Structures
- Objects
- Queries
- And much more
Changing a Variable
Variables can change during program execution.
Variables Can Use Expressions
Variables Can Store Function Results
<cfscript>
today = now();
writeOutput(today);
</cfscript>now() returns the current date and time, which is stored in the today variable.
Variable Naming Rules
A variable name:
- Must begin with a letter or underscore (_)
- Can contain letters, numbers, and underscores
- Cannot contain spaces
- Cannot use special characters like @, #, %
- Should have meaningful names
Good examples:
firstName employeeId totalAmount isLoggedIn customerEmail
Bad examples:
123name first name total-price @email
Naming Convention
Use camelCase for variable names.
firstName lastName mobileNumber totalPrice
Avoid:
firstname FIRSTNAME First_Name
Consistent naming makes your code easier to read.
Real-World Example
Variable Lifecycle
Variables exist while your application is running. Later in the course, you'll learn about scopes that determine how long variables live.
Common Beginner Mistakes
Using a variable before creating it
writeOutput(name) fails if name hasn't been assigned yet. Create it first — name = "Sandeep"; — then writeOutput(name);
Using meaningless names
Names like a, x, temp1, or abc don't say what they hold. Prefer something descriptive, like employeeName, customerId, or orderTotal.
Forgetting quotes around text
name = Sandeep; treats Sandeep as a variable reference, not text — it needs quotes: name = "Sandeep";
Best Practices
- Use meaningful names.
- Keep one purpose per variable.
- Follow camelCase.
- Don't reuse variables for unrelated data.
- Prefer readable code over short names.
Interview Questions
What is a variable?
A named container used to store data during program execution.
Which operator creates a variable in CFScript?
=
Can a variable's value change?
Yes.
Can variables store numbers and text?
Yes. They can store many different data types.
What naming convention is commonly used?
camelCase.
Summary
In this lesson, you learned that variables are the foundation of every ColdFusion program. They allow you to store, update, and reuse data throughout your application. Whether you're displaying user information, calculating totals, or calling functions, variables make your code more flexible and maintainable.
What's Next?
The next lesson covers Data Types — the different kinds of values a ColdFusion variable can hold, from strings and numbers through to arrays, structures, and queries.