Contents

Roo

Array.first()

Used to return either the first or the first n elements of this array. This array is unaltered.

array.first() as (Object or Nothing)
array.first(n as Number) as Array

Parameters

  • n. Integer Number. The number of elements from this array to retrieve. Retrieval begins from index 0.

Returns

  • If this array is empty then the first form returns Nothing and the second form returns an empty Array object
  • If n > array.length then this array is returned
  • Otherwise returns a new Array object containing the requested number of elements

Example

var x = ["a", "b", "c", "d"]
x.first() # => "a"
x.first(1) # =>["a"]
x.first(2) # => ["a", "b"]
x.first(3) # => ["a", "b", "c"]
x.first(4) # => ["a", "b", "c", "d"]
x.first(5) # => ["a", "b", "c", "d"]
var y = []
y.first() # => Nothing
y.first(1) # => []