DevLearningTools

🚧 This site is under active construction — new tools, guides, and pages are added every week.

MODULE 2 · LESSON 01

Variables

What a variable is, how to create and update one in both CFScript and Tag Syntax, naming rules and conventions, and where variables fit into a real ColdFusion page.

New lessons are added one at a time as the course gets built out — a graded quiz for each lesson is still on the way.

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.

VariableValue
name"Sandeep"
age28
isLoggedIntrue
NOTE

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
<cfscript>

    name = "Sandeep";
    age = 28;
    city = "Faridabad";

</cfscript>

Here:

  • name stores "Sandeep"
  • age stores 28
  • city stores "Faridabad"

Displaying Variables

Use writeOutput().

cfscript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Creating Variables Using Tag Syntax

tag syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

Both styles produce the same result.

Variables Can Store Different Types of Data

cfscript
<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.

cfscript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Variables Can Use Expressions

cfscript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Variables Can Store Function Results

cfscript
<cfscript>

    today = now();

    writeOutput(today);

</cfscript>
NOTE

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:

good
firstName
employeeId
totalAmount
isLoggedIn
customerEmail

Bad examples:

bad
123name
first name
total-price
@email

Naming Convention

Use camelCase for variable names.

good
firstName
lastName
mobileNumber
totalPrice

Avoid:

avoid
firstname
FIRSTNAME
First_Name
NOTE

Consistent naming makes your code easier to read.

Real-World Example

cfscript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

Variable Lifecycle

Create Variable
Store Value
Use Variable
Update Value (Optional)
Program Ends
NOTE

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.