Whatever message this page gives is out now! Go check it out!

ArrayGetAt

Last update:
Jun 5, 2026
Retrieves an element from an array using positive or negative indexing with bounds checking.

Description

The function provides safe array element access and supports negative indexing, allowing access from the end of the array.
Unlike standard bracket notation, this function supports both forward and reverse indexing and provides descriptive error messages for invalid indices.

Syntax

ArrayGetAt(Array array, Integer index)

Member Function Syntax

array.at(Integer index)
ParameterTypeDescription
arrayArray (required)The target array to access.
indexInteger (required)1-based positive index or negative index. Positive indices count from the start; negative indices count from the end.

Return Value

Any: The element at the specified position.

Exceptions

Throws an error if the index is out of bounds (for example, Invalid array index: 10. Array size is 4.).

Usage Notes

  • Negative indexing: -1 returns the last element, -2 returns the second-to-last.
  • Supports 1D, 2D, and 3D arrays.
  • Member function syntax array.at() is equivalent to ArrayGetAt().

Positive Indexing


<cfscript>
arr = ['apple','banana','cherry','date'];
writeOutput(ArrayGetAt(arr,1));
writeOutput(ArrayGetAt(arr,3));
</cfscript>

Negative Indexing


<cfscript>
arr = ['apple','banana','cherry','date'];
writeOutput(ArrayGetAt(arr,-1));
writeOutput(ArrayGetAt(arr,-2));
</cfscript>

Member Function Syntax


<cfscript>
arr = ['apple','banana','cherry','date'];
writeOutput(arr.at(-1));
writeOutput(arr.at(1));
</cfscript>

Error Handling: Out of Bounds


<cfscript>
arr = ['apple','banana','cherry','date'];
try {
  ArrayGetAt(arr,10);
} catch(any e) {
  writeOutput(e.message);
}
</cfscript>

2D Array Access


<cfscript>
a1 = ArrayNew(2);
a1[1][1] = "one-one";
a1[2][2] = "two-two";
writeOutput(ArrayGetAt(a1,1)[1]);
</cfscript>

3D Array Navigation


<cfscript>
a3 = ArrayNew(3);
a3[1][1][1] = 100;
result = ArrayGetAt(ArrayGetAt(ArrayGetAt(a3,1),1),1);
writeOutput(result);
</cfscript>

Share this page

Was this page helpful?
We're glad. Tell us how this page helped.
We're sorry. Can you tell us what didn't work for you?
Thank you for your feedback. Your response will help improve this page.

On this page