Arrays

Arrays are ordered, growable, and can mix value types freely.

let nums = [1, 2, 3];
let mixed = [1, "two", true];
output(nums[0]);   // 1 -zero-based indexing
output(nums[-1]);  // 3 -negative indices count from the end
nums[1] = 20;    // index assignment

An out-of-range index raises a runtime error rather than returning null:

let a = [1, 2, 3];
output(a[5]); // runtime error: Index '5' is invalid for array with size '...'
The size number quoted in that specific error message doesn't reliably reflect the array's actual length -a diagnostic-formatting quirk, not a problem with the bounds check itself, which does correctly reject the out-of-range index. Full trace of exactly why on E1017 in the errors reference.

size

The element count is a property, not a method -no parentheses:

output([1,2,3].size); // 3

Iteration

foreach (n: [10, 20, 30]) {
    output(n);
}

Methods

push(value) mutates

Appends value to the end. Returns null.

pop() mutates

Removes and returns the last element.

insert(index, value) mutates

Inserts value at index, shifting later elements up. Returns null.

remove(index) mutates

Removes the element at index and returns it.

clear() mutates

Empties the array in place. Returns null.

reverse() mutates

Reverses the array in place. Returns null.

sort() mutates

Sorts the array in place using each element's natural ordering (numbers only -see the comparison caveat; sorting an array containing strings will raise the same "cannot compare" error < does). Returns the array itself, so it can be chained.

contains(value) → boolean

Whether any element equals value (by the same rules as ==).

isEmpty() → boolean

Whether the array has zero elements. Prefer this over relying on array truthiness -see Truthiness.

copy() → array

Returns a new array with the same elements. Use this before mutating if you need to keep the original untouched -assigning an array to another variable does not copy it.

let a = [3, 1, 2];
a.sort();
output(a);          // [1,2,3]
a.push(10);
output(a.pop());     // 10
a.insert(0, 99);
output(a.remove(1)); // 1
output(a);           // [99,2,3]
output(a.contains(99)); // true

How arrays print

output() on an array uses a compact, JSON-like form with no spaces after commas, and strings inside it keep their quotes (unlike a bare string passed to output() directly):

output([1, 2, 3]);   // [1,2,3]
output(["a", "b"]);   // ["a","b"]
output("a");           // a -no quotes when the string itself is the whole value