Basic JavaScript Question:
Download Questions PDF

How to shift and unshift using JavaScript?

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 Interview Questions And Answers PDF

Previous QuestionNext Question
How to make a array as a stack using JavaScript? How to create an object using JavaScript?