push() / pop()

VDX provides built-in functions for modifying arrays: push() to add elements and pop() to remove them.

push()

Appends a value to the end of an array:

push(array, value)

push() example

let arr = [1, 2];
push(arr, 3);
print(arr);       // [1, 2, 3]

push(arr, 4);
print(arr);       // [1, 2, 3, 4]

pop()

Removes and returns the last element from an array:

pop(array)  // Returns the removed element

pop() example

let arr = [10, 20, 30];
let last = pop(arr);

print(last);      // 30
print(arr);       // [10, 20]

Stack operations

Combined, push() and pop()enable stack (LIFO) behavior:

class Stack {
    let items = [];
    
    fn pushItem(item) {
        push(items, item);
    }
    
    fn popItem() {
        if (len(items) == 0) {
            return "empty";
        }
        return pop(items);
    }
    
    fn size() {
        return len(items);
    }
}

Important notes

  • Both functions modify the array in-place
  • pop() on an empty array throws an error
  • The array must be a variable (not a literal): push([1,2], 3) is invalid