- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
Advertisements