Arrays in ColdFusion are dynamic — you don't need to declare a size up front, and functions exist to add or remove elements at the start, end, or anywhere in between.
Learning Objectives
After completing this lesson, you'll be able to:
- Add elements to the end or start of an array.
- Insert an element at a specific position.
- Remove elements by value or by position.
- Use the stack/queue-style push, pop, shift, and unshift functions.
- Replace a range of elements with arraySplice.
Adding to the End: arrayAppend
Adding to the Start: arrayPrepend
Inserting at a Position: arrayInsertAt
Removing by Value: arrayDelete
arrayDelete removes the first element matching that exact value. Use arrayDeleteNoCase for a case-insensitive match on strings.
Removing by Position: arrayDeleteAt
Removing Everything: arrayClear
Stack/Queue-Style Functions
These four functions are shorthand for common patterns: treating an array like a stack (last in, first out) or a queue (first in, first out).
| Function | Same as | Effect |
|---|---|---|
| arrayPush(arr, val) | arrayAppend | Adds to the end |
| arrayPop(arr) | arrayDeleteAt(arr, arrayLen(arr)) | Removes and returns the last element |
| arrayUnshift(arr, val) | arrayPrepend | Adds to the start |
| arrayShift(arr) | arrayDeleteAt(arr, 1) | Removes and returns the first element |
Setting a Range: arraySet
Sets every element in a given index range to the same value — useful for initializing an array before filling it in another loop.
Replacing a Range: arraySplice
The most flexible of these functions — it can remove elements, insert elements, or both, starting at a given position, in one call.
A 0 for the removal count means "remove nothing, just insert" — that's what makes the first example a pure insertion.
Swapping Two Elements: arraySwap
Real-World Example: Managing a To-Do List
Common Beginner Mistakes
Forgetting these functions modify the array in place
arrayAppend(fruits, "Mango") changes fruits directly — it doesn't return a new array you need to reassign. Writing fruits = arrayAppend(fruits, "Mango") is unnecessary (though harmless, since it does also return the array).
Confusing arrayDelete with arrayDeleteAt
arrayDelete removes by matching value; arrayDeleteAt removes by position (index). Using the wrong one either removes nothing (no match found) or removes the wrong element.
Getting the arraySplice removal count wrong
The third argument is how many elements to remove starting at that position — passing the wrong count either leaves old elements behind or deletes more than intended.
Best Practices
- Use arrayAppend / arrayPrepend for the common cases; reach for arrayInsertAt or arraySplice only when you need a specific middle position.
- Use arrayDeleteAt when you know the position, arrayDelete when you only know the value.
- Use arraySplice when you need to both remove and insert in a single operation, rather than chaining multiple calls.
Interview Questions
What's the difference between arrayDelete and arrayDeleteAt?
arrayDelete removes the first element matching a given value. arrayDeleteAt removes the element at a specific index, regardless of its value.
What does arrayPop do?
Removes and returns the last element of an array.
How would you insert an element into the middle of an array without removing anything?
arrayInsertAt(arr, position, value), or arraySplice(arr, position, 0, [value]) — the 0 means remove nothing.
Summary
In this lesson, you learned how to add and remove array elements with arrayAppend, arrayPrepend, arrayInsertAt, arrayDelete, arrayDeleteAt, and arrayClear, the stack/queue-style push, pop, shift, and unshift, and the more flexible arraySplice and arraySwap.
What's Next?
The next lesson covers searching and sorting arrays — arrayFind, arrayContains, arraySort, and arrayReverse.