DevLearningTools

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

MODULE 5 · LESSON 03

Adding & Removing Array Elements

Modifying ColdFusion arrays — arrayAppend, arrayPrepend, arrayInsertAt, arrayDelete, arrayDeleteAt, arrayClear, arraySplice, and the stack/queue-style push, pop, shift, and unshift.

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.

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

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

Adding to the Start: arrayPrepend

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

Inserting at a Position: arrayInsertAt

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

Removing by Value: arrayDelete

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

arrayDelete removes the first element matching that exact value. Use arrayDeleteNoCase for a case-insensitive match on strings.

Removing by Position: arrayDeleteAt

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

Removing Everything: arrayClear

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

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

FunctionSame asEffect
arrayPush(arr, val)arrayAppendAdds to the end
arrayPop(arr)arrayDeleteAt(arr, arrayLen(arr))Removes and returns the last element
arrayUnshift(arr, val)arrayPrependAdds to the start
arrayShift(arr)arrayDeleteAt(arr, 1)Removes and returns the first element
CFScript
Simulated output — illustrative only, not a live ColdFusion/Lucee server.

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.

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

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.

CFScript — insert without removing
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
CFScript — replace a range
Simulated output — illustrative only, not a live ColdFusion/Lucee server.
NOTE

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

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

Real-World Example: Managing a To-Do List

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

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.