← Back to Docs

Arrays

Arrays let you store multiple values in a single variable. Declare them with let and square brackets.

Creating arrays

let numbers = [1, 2, 3];
let names = ["Alice", "Bob", "Charlie"];
let empty = [];

Index access

Access elements by index (starting at 0):

let arr = [10, 20, 30];
print(arr[0]);    // 10
print(arr[2]);    // 30

Index assignment

Change an element by assigning to an index:

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

len()

Get the length of an array with the built-in len() function:

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

len() also works on strings: len("hello") returns 5.

push()

Append a value to an array with push():

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

Printing arrays

let arr = [1, "two", 3];
print(arr);       // [1, "two", 3]

Truthiness

Non-empty arrays are truthy, empty arrays are falsy:

let arr = [1];
if (arr) {
    print("has items");
}

Out of bounds

⚠️ Accessing an index outside the array range throws a runtime error with the index and array size shown.