Basic JavaScript Question:

How to shift and unshift using JavaScript?

Tweet Share WhatsApp

Answer:

<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 in JavaScript. Queues are easily implemented using combinations.

Download JavaScript PDF Read All 114 JavaScript Questions
Previous QuestionNext Question
How to make a array as a stack using JavaScript? How to create an object using JavaScript?