ArrayShift

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.

Method Signature

ArrayShift(array=[modifiablearray], defaultValue=[any])

Arguments

Argument
Type
Required
Description
Default

array

modifiablearray

true

The array to shift

defaultValue

any

false

Examples

Example with simple values

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

Run Example

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

Result: 1

Using a member function

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

Run Example

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

Result: 1

Additional Examples

Run Example

numbers = [ 
	1,
	2,
	3,
	4
];
ArrayShift( numbers );
Dump( numbers ); // Outputs 1
moreNumbers = [
	5,
	6,
	7,
	8
];
ArrayShift( moreNumbers );
Dump( numbers ); // Outputs 5
numbers = [
	"one",
	"two",
	"three",
	"four"
];
ArrayShift( numbers );
Dump( numbers );
 // Outputs one

Last updated

Was this helpful?