The Array method shift() in Apps Script

Last updated: February 10, 2025

The Array method shift() removes the first element from an array and returns it.

Syntax

array.shift()

Parameters

Not applicable.

Return value

The shift() method returns the first element from an array. If the array is empty, the shift() method returns null.

Example

The code below uses the shift() array method to remove elements from the beginning of the array colors.

var colors = ["red", "blue"];
Logger.log(colors);
Logger.log(colors.length);
var color1 = colors.shift();
Logger.log(color1);
Logger.log(colors);
Logger.log(colors.length);
var color2 = colors.shift();
Logger.log(color2);
Logger.log(colors);
Logger.log(colors.length);
var color3 = colors.shift();
Logger.log(color3);
Logger.log(colors);
Logger.log(colors.length);

Output

[red, blue]

2.0

red

[blue]

1.0

blue

[]

0.0

null

[]

0.0

Conclusion

In this tutorial, you learned how to use the shift() array method to remove elements from the beginning of an array.

DISCLAIMER: This content is provided for educational purposes only. All code, templates, and information should be thoroughly reviewed and tested before use. Use at your own risk. Full Terms of Service apply.

Small Scripts, Big Impact

Join 1,500+ professionals who are supercharging their productivity with Google Sheets automation

Exclusive Google Sheets automation tutorials and hands-on exercises
Ready-to-use scripts and templates that transform hours of manual work into seconds
Email updates with new automation tips and time-saving workflows

By subscribing, you agree to our Privacy Policy and Terms of Service