» Language: Lists

Lists are a collection of zero or more values.

Lists can be created using by wrapping values in [] and separating them by commas. An optional trailing comma is allowed. List elements can be differing types. Examples:

[]                  // An empty list
["foo"]             // Single element list
["foo", 1, 2, true] // Multi element list with different types
["foo", [1, 2]]     // List containing another list

A list can be sliced to create sublists. The set operators can be used to test for value inclusion in a list.

» Accessing Elements

List elements can be accessed with the syntax name[index] where index is zero-indexed.

A negative index accesses the list in reverse. It is the same as reversing a list and then using a positive index. Similar to a positive index, it is bounded by the length of the list as a negative value.

Accessing beyond the length of the list results in undefined.

Examples:

list = ["foo", 1, true, [1, 2]]

list[0]    // "foo"
list[2]    // true
list[4]    // undefined
list[-2]   // true
list[-4]   // "foo"
list[-5]   // undefined
list[3][1] // 2

» List Append

Values can be appended to a list using the built-in function append.

This modifies the list in-place and returns undefined. For more information on why append behaves this way, please read the full documentation in the built-in functions page.

append([1,2], 3)      // [1, 2, 3]
append([1,2], "foo")  // [1, 2, "foo"]
append([1,2], [3])    // [1, 2, [3]]
append(1, 3)          // error()

» List Concatenation

Two lists can be concatenated using the + operator or the shorthand += assignment operator. For the + operator, a new list is returned. For +=, the left-hand list is modified in place.

Examples:

[1] + [2]   // [1, 2]
[1] + [[1]] // [1, [1]]
[1] + 1     // error

a = [1]
a += [2]    // a = [1, 2]
a += 3      // error

» List Length

The length of a list can be retrieved using the built-in function length.

Examples:

length([])      // 0
length(["foo"]) // 1