DZone
Web Dev Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Web Dev Zone > Queue Data Structure in JavaScript

Queue Data Structure in JavaScript

A basic tutorial detailing how Queues work in JavaScript, with some code snippets to help you out.

Unni Mana user avatar by
Unni Mana
CORE ·
Dec. 15, 16 · Web Dev Zone · Tutorial
Like (1)
Save
Tweet
4.55K Views

Join the DZone community and get the full member experience.

Join For Free

JavaScript is very suitable for developing data structures and in fact in my opinion it is very easy.  Let’s us develop a “Queue” data structure in Java Script

By definition, Queue is a First-in-First-Out data structure, which means that it will maintain the insertion order. For example, if we insert 1, 2, 3 into a queue data structure, then it will output 1, 2, and 3 in the same order.

So we will develop a simple queue here.  In order to develop a queue, we can use an array. The array will hold the entire inserted item as shown below:

var arr = [];

The functionality of a Queue is that when you get an item from it, it will return the top element and remove it from the Queue. This is the default behavior of a Queue, which will put the next element on the top of the queue.

We can start with a  function called "Queue":

function Queue() {
	this.arr = [];
}

Also, we need two methods: one is add () and the other one is get ().

add() will insert an object into the array, which is shown below:

function add(item){
   arr.push(item);
}

get() will get the item from the array, which is shown below:

function get() {
   return arr.splice(0,1);
}

Please take a look at the splice() method with arguments. This method can either remove or add elements to the array. In our case, since the first argument is 0, then it will remove elements instead of adding them. The second argument is 1, which will remove exactly one element from the array. This removal happens from the top of the array.

This way, you can develop a queue in JavaScript which can be used in many scenarios. The above piece of code can also be further extended.

You can take a look at this video for more information.

Data structure Data (computing) JavaScript

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Implementing Microservices Architectures
  • Testing Your Infrastructure as Code Using Terratest
  • How to Build a Simple CLI With Oclif
  • Kubernetes Service Types Explained In-Detail

Comments

Web Dev Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo