The Array method pop() in Apps Script

The Array method pop() removes the last element from an array and returns it. .

Syntax

array.pop()

Parameters

Not applicable.

Return value

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

Example

The code below uses the pop() array method to remove elements from the array colors.

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

Output

[red, blue]

2.0

blue

[red]

1.0

red

[]

0.0

null

[]

0.0

Conclusion

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

Stay up to date

Follow me via email to receive actionable tips and other exclusive content. I'll also send you notifications when I publish new content.
By signing up you agree to the Privacy Policy & Terms.

Have feedback for me?

I'd appreciate any feedback you can give me regarding this post.

Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!