How to shift and unshift using JavaScript?

Q

How to shift and unshift using JavaScript?

✍: Guest

A
<script type="text/javascript">
var numbers = ["one", "two", "three", "four"];
numbers.unshift("zero");
document.write(" "+numbers.shift());
document.write(" "+numbers.shift());
document.write(" "+numbers.shift());
</script>

This produces

zero one two

shift, unshift, push, and pop may be used on the same array. Queues are easily implemented using combinations.

2011-08-02, 3403👍, 0💬