
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Peeking elements from a Queue in Javascript
Peeking a Queue means getting the value at the head of the Queue. So we can implement the peek function as follows −
EXample
peek() { if (isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; }
You can check if this function is working fine using −
Example
let q = new Queue(2); q.enqueue(3); q.enqueue(4); console.log(q.peek()); q.display();
Output
This will give the output −
3 [ 3, 4 ]
As you can see here, peek() differs from dequeue in that it just returns the front value without removing it.
- Related Articles
- Peeking elements from a Stack in Javascript
- Peeking elements from a PriorityQueue using JavaScript
- Remove elements from a queue using Javascript
- Add elements to a Queue using Javascript
- Clearing the elements of the Queue in Javascript
- Creating a Queue in Javascript
- Popping elements from a Stack in Javascript
- Queue Data Structure in Javascript
- The Queue Class in Javascript
- The Priority Queue in Javascript
- Implementation of Queue in JavaScript
- Creating a Priority Queue using Javascript
- How to push and pop elements in a queue in Ruby?
- Remove an element from a Queue in Java
- Create a Queue from another collection in C#?

Advertisements