[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


I need to create a list in JavaScript where I can pop the top item of the list

Posted on 2011-10-02
Medium Priority
Last Modified: 2012-05-12
I have used arfrays in JavaScript, but thought I saw a different list where I can pop an item of the list and append a new one to the bottom?

Question by:newbieweb
LVL 15

Accepted Solution

Eyal earned 2000 total points
ID: 36901591
<title>FIFO Queue</title>
<script type="text/javascript">
// For: http://www.webdeveloper.com/forum/showthread.php?t=220340

Array.prototype.store = function (info) {
Array.prototype.fetch = function() {
  if (this.length <= 0) { return ''; }  // nothing in array
  return this.shift(); 
Array.prototype.display = function() {
  return this.join('\n');
var FIFO = new Array();

function StoreEntry() {
	var sel = document.getElementById('entry');
	sel.value = '';
function FetchEntry() {
	document.getElementById('entry').value = FIFO.fetch();
<h1>FIFO Queue Example</h1>
<input type="text" id="entry">
<button onclick="StoreEntry()">Store</button>
<button onclick="FetchEntry()">Fetch</button>
<button onclick="alert(FIFO.display())">Display</button>

Open in new window


Author Closing Comment

ID: 36928217

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question