Skip to content

Instantly share code, notes, and snippets.

@Damimd10
Created February 6, 2019 00:28
Show Gist options
  • Select an option

  • Save Damimd10/cd9a4a4ad0bbfed17dc6946d6e189a72 to your computer and use it in GitHub Desktop.

Select an option

Save Damimd10/cd9a4a4ad0bbfed17dc6946d6e189a72 to your computer and use it in GitHub Desktop.
JavaScript Queue
function createQueue() {
const queue = [];
return {
enqueue(item) {
queue.unshift(item)
},
dequeue() {
return queue.pop()
},
get length() {
return queue.length
},
isEmpty() {
return queue.length == 0
}
}
}
const myQueue = createQueue();
myQueue.isEmpty() // true
myQueue.enqueue('Make a sandwich')
myQueue.enqueue('Help others learn queue')
myQueue.enqueue('Be happy')
myQueue.length() // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment