Removes the first element from an array and returns the removed element. This method changes the length of the array. If used on an empty array, an exception will be thrown.

arrayShift(array) → returns any

Member Function Syntax


Argument Reference

array array

An array of elements from which the first will be removed and returned.



Version 5.3.8+ Lucee allows an additional parameter, defaultValue of type any, that is returned if the array is empty.

Sample code invoking the arrayShift function

Take an array of numbers and shift the first one off.

arr = [ 1, 2, 3 ];
el = arrayShift(arr);
writeOutput( el );

Expected Result: 1

This is the same example as above, but using a member function on the array instead of a standalone function.

arr = [ 1, 2, 3 ];
el = arr.shift();
writeOutput( el );

Expected Result: 1

Signup for cfbreak to stay updated on the latest news from the ColdFusion / CFML community. One email, every friday.

Fork me on GitHub