Pardon the mess, Play My Code is in beta!

READY TO PLAY?
CLICK TO LOG IN!

sign up - lost password

unshift

.unshift( val )

Like add, this adds an element to the array. However it adds the element to the beginning instead of the end.

It is called 'unshift' because it is the opposite of 'shift', which removes from the beginning of the array.

/*
 * Creates an array containing the numbers
 * from 9 through to 0,
 * in that order,
 * by adding the numbers at the beginning.
 */
ns = []
10.times() do |i|
    ns.unshift( i )
end

console( ns )

See Also