The Array method unshift() in Apps Script

Last updated: February 10, 2025

The Array method unshift() adds one or more elements to the beginning of an array and returns its new size.

Syntax

array.unshift(val, val2, …, val3)

Parameters

One or more values to be added to the beginning of the array.

Return value

The length of the array after adding elements to it.

Examples

The code below uses the unshift() array method to add elements to the beginning of the array colors.

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

// Adding just one element
var length = colors.unshift('purple');
Logger.log(colors);
Logger.log(length);

// Adding two elements
length = colors.unshift('green', 'yellow');
Logger.log(colors);
Logger.log(length);

Output

[red, blue]

2.0

[purple, red, blue]

3.0

[green, yellow, purple, red, blue]

5.0

Conclusion

In this tutorial, you learned how to use the unshift() array method to add elements to 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