Quirrel4.41.0

types.Array.top

Method on every array
array.top(): any

Returns the last element without removing it.

Return value

The last element.

Errors

Throws top() on a empty array when the array has no elements.

Notes

pop reads the same element and removes it.

Example

examples/types/array/top.nut
let a = [1, 2, 3];
println("a.top() =", a.top());   // peeks the last element
println("a.len() =", a.len());    // unchanged: top() does not remove anything
try { [].top() } catch (e) { println("[].top() throws:", e); }
Output:
a.top() = 3
a.len() = 3
[].top() throws: top() on a empty array

See also

popRemoves and returns the last element.
lenReturns the number of elements in the array.
arrayclass index