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 '...'
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
Appends value to the end. Returns null.
Removes and returns the last element.
Inserts value at index, shifting later elements up. Returns null.
Removes the element at index and returns it.
Empties the array in place. Returns null.
Reverses the array in place. Returns null.
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.
Whether any element equals value (by the same rules as ==).
Whether the array has zero elements. Prefer this over relying on array truthiness -see Truthiness.
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