Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arrays

NaijaScript arrays hold heterogeneous values and are written with square brackets.

Literals

Declare arrays:

make foo get ["apple", "banana", "orange"]
make bar get [1, 2, 3, 4]
make mixed get ["text", 42, true]
make nested get [[1, 2], [3, 4]]

Indexing and assignment

Access elements with zero-based indexes and assign by indexing:

make foo get ["a", "b", "c"]
make first get foo[0]    # "a"
foo[1] get "bee"         # foo becomes ["a", "bee", "c"]

Nested arrays use chained indexes:

make nested get [[1,2], [3,4]]
make val get nested[1][0]   # 3
nested[0][1] get 9          # nested becomes [[1,9], [3,4]]

Methods

Available methods and return types:

MethodDescriptionReturns
len()Number of elements in the arrayNumber
push(value)Append value to the end of the arrayArray
pop()Removes the last element of the array (if any)Array

Note: push and pop mutates the array and yields the array for chaining.