Array shift()

In this chapter you will learn:

  1. How to use array shift method

shift():Array Queue Methods

JavaScript array can act as a queue. Queues restrict access in a first-in-first-out (FIFO) data structure. A queue adds items to the end of a list and retrieves items from the front of the list.

The array method for retrieving from a queue is called shift(). shift() removes the first item in the array and returns it decreases the length of the array by one.

<!DOCTYPE html><!--   ja va 2 s  .c  om-->
<html>
<head>
    <script type="text/javascript">
        var colors = new Array(); //create an array 
        var count = colors.push("A", "B"); //push two items 
        document.writeln(count); //2 
        count = colors.push("C"); //push another item on 
        document.writeln(count); //3 
        
        var item = colors.shift(); //get the first item 
        document.writeln(item); //"A" 
        document.writeln(colors.length); //2 
       
    </script>
</head>
<body>
</body>
</html>

Click to view the demo

Next chapter...

What you will learn in the next chapter:

  1. How to reduce array to a value
Home » Javascript Tutorial » Array
Array Type
Array creation
Array type detecting
Array iterate
Array Length
Add to Array
Array join
Array concat()
Array every method
Array search from start with indexOf()
Array search from the end with lastIndexOf()
Array filter
Array mapping
Array forEach
Array pop and push
Array shift()
Array reduce()
Array reduceRight()
Array reverse()
Array slice()
Array some()
Array splice()
Array sort()
Array toString()
Array unshift()