APIs

This page provides API specifications of array.

Properties

.length

Return the number of elements in an array.

Example

Get length of an array

var nums = [1, 2, 3, 4]
puts nums.length    // 4

Get the last element in array

var arrOS = ['Windows', 'Linux', 'OSX']
puts arrOS[arrOS.length]    // OSX

Iterate through an array

var arr = [2, 4, 8, 10]
for i = 0, arr.length - 1 {
    puts arr[i]
}

.length is getter only, cannot assign value for this property.

Methods

.push(value)

Push value to an array

Parameters (1)

value : any

  • A value to be pushed

Return value

This method return the index of last pushed value in array.

var arr = [1, 2]
puts arr.push(5)    // 2
puts arr            // [1, 2, 5]

.pop()

Remove the last element of array

Parameters (0)

Return value

The last element of array.

var arr = [4, 5, 6]
puts arr.pop()    // 6
puts arr.length   // 2
puts arr          // [4, 5]

.shift()

Remove the first element of array.

Parameters (0)

Return value

The first element of array.

var arr = [7, 8, 9]
puts arr.shift()    // 7
puts arr        

Last updated