DevLearningTools

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

MODULE 5 · LESSON 01

Arrays

ColdFusion arrays — an ordered, 1-indexed list of values. Creating, accessing, modifying, and checking arrays in both CFScript and Tag Syntax.

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.

An array is an ordered list of values, stored under a single variable name and accessed by position. Arrays are everywhere in real applications — a shopping cart's items, a list of search results, a set of email recipients.

ColdFusion arrays are dynamic — you don't declare a fixed size up front, and rows can grow or shrink as needed.

Learning Objectives

After completing this lesson, you'll be able to:

  • Create an array using literal syntax and arrayNew().
  • Access and modify array elements by index.
  • Use negative indices to access elements from the end.
  • Get an array's length with arrayLen().
  • Check whether a value is an array with isArray().

What Is an Array?

An array holds a series of values in order, each reachable by its position — called its index. ColdFusion arrays are 1-indexed: the first element is at position 1, not 0.

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

This trips up developers coming from JavaScript, Python, or Java, where arrays start at 0. In CFML, fruits[1] is the first element.

Creating Arrays

The cleanest way to create an array is literal syntax — square brackets with values separated by commas. arrayNew() is the older, more explicit alternative.

CFScript — literal syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
CFScript — arrayNew()
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
Tag Syntax
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

The 1 passed to arrayNew(1) means one dimension — a plain list. Multi-dimensional arrays are covered in the next lesson.

Accessing Elements

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

Negative Indices

A negative index counts from the end of the array — arr[-1] is the last element, arr[-2] is the second-to-last, and so on.

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

Modifying Elements

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

Array Length

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

Looping Through an Array

Covered in full in the Loops module — a quick recap here, since it's one of the most common things you'll do with an array.

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

Checking for an Array

isArray() checks whether a value is actually an array — useful when a variable's type isn't guaranteed, like data coming from a function argument or an external source.

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

Real-World Example: Shopping Cart

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

Common Beginner Mistakes

Assuming arrays start at index 0

fruits[0] doesn't exist in CFML and throws an error. The first element is always fruits[1].

Accessing an index that doesn't exist

fruits[10] on a 3-element array throws an error rather than returning a blank or null value. Check arrayLen(fruits) or use arrayIsDefined() first if the index isn't guaranteed to exist.

Confusing arrays with lists

An array is a real data structure with its own functions (arrayLen, arrayAppend, etc.). A list is just a delimited string like "a,b,c" — similar in spirit, but a different type with a different set of functions.

Best Practices

  • Prefer array literal syntax ([...]) over arrayNew(1) for readability, unless you specifically need an empty array of a known dimension.
  • Use isArray() when a variable's type isn't guaranteed before treating it as an array.
  • Use negative indices (arr[-1]) instead of arr[arrayLen(arr)] when you just need the last element.

Interview Questions

What index does the first element of a ColdFusion array have?

1 — ColdFusion arrays are 1-indexed, not 0-indexed.

How do you access the last element of an array without knowing its length?

Use a negative index — arr[-1].

What happens if you access an index beyond an array's length?

ColdFusion throws an error, rather than silently returning a blank or undefined value.

How do you check if a variable is an array?

isArray(variable) returns true or false.

Summary

In this lesson, you learned what a ColdFusion array is, how to create one with literal syntax or arrayNew(), how 1-based and negative indexing work, and how to access, modify, and check arrays.

What's Next?

The next lesson covers multi-dimensional arrays — arrays of arrays, useful for table-like or grid-shaped data.