Found 10483 Articles for Web Development

Queue Data Structure in Javascript

Sai Teja Kotha
Updated on 16-Dec-2022 15:55:25

607 Views

In this article, we are going to discuss the queue data structure in JavaScript. It is a linear data structure where the enqueue and dequeue of elements follow the FIFO (first in first out sequence). The queue is open at both ends. One end is always used to insert data and the other is used to remove data. Here, we use two pointers rear and front. The rear is used to insert the data and the front is used to remove the data. A real-world example of the queue can be a single-lane one-way road, where the vehicle enters first, ... Read More

The Stack Class in Javascript

Samual Sam
Updated on 15-Jun-2020 08:29:05

231 Views

Here is the complete implementation of the Stack class −Exampleclass Stack {    constructor(maxSize) { // Set default max size if not provided       if (isNaN(maxSize)) {          maxSize = 10;       }       this.maxSize = maxSize; // Init an array that'll contain the stack values.       this.container = [];    }      display() {       console.log(this.container);    }      isEmpty() {       return this.container.length === 0;    }      isFull() {       return this.container.length >= this.maxSize;    }   ... Read More

Clearing the elements of a Stack in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 08:31:33

961 Views

Consider a simple stack class in Javascript. Exampleclass Stack {    constructor(maxSize) {       // Set default max size if not provided       if (isNaN(maxSize)) {          maxSize = 10;       }       this.maxSize = maxSize; // Init an array that'll contain the stack values.       this.container = [];    }    // A method just to see the contents while we develop this class    display() {       console.log(this.container);    }    // Checking if the array is empty    isEmpty() {   ... Read More

Peeking elements from a Stack in Javascript

Samual Sam
Updated on 15-Jun-2020 08:34:42

759 Views

Consider a simple stack class in Javascript. Exampleclass Stack {    constructor(maxSize) {       // Set default max size if not provided       if (isNaN(maxSize)) {          maxSize = 10;       }       this.maxSize = maxSize; // Init an array that'll contain the stack values.       this.container = [];    }    // A method just to see the contents while we develop this class    display() {       console.log(this.container);    }    // Checking if the array is empty    isEmpty() {   ... Read More

Popping elements from a Stack in Javascript

karthikeya Boyini
Updated on 15-Jun-2020 08:38:10

298 Views

Consider a simple stack class in Javascript.Exampleclass Stack {    constructor(maxSize) {       // Set default max size if not provided       if (isNaN(maxSize)) {          maxSize = 10;       }       this.maxSize = maxSize; // Init an array that'll contain the stack values.       this.container = [];    }    // A method just to see the contents while we develop this class    display() {       console.log(this.container);    }    // Checking if the array is empty    isEmpty() {   ... Read More

Pushing elements to a Stack in Javascript

Samual Sam
Updated on 15-Jun-2020 08:40:11

261 Views

Consider the following stack class in Javascript with few small helper functions.Exampleclass Stack {    constructor(maxSize) {       // Set default max size if not provided       if (isNaN(maxSize)) {          maxSize = 10;       }       this.maxSize = maxSize; // Init an array that'll contain the stack values.       this.container = [];    }    // A method just to see the contents while we develop this class    display() {       console.log(this.container);    }    // Checking if the array is ... Read More

Creating a Stack in Javascript

Sai Teja Kotha
Updated on 08-Dec-2022 12:04:00

2K+ Views

In this article, we are going to discuss how to create the stack data structure in JavaScript. It is a linear data structure where the push and popping of elements follow the LIFO (last in first out and FILO (first in last out) sequence. Though Arrays in JavaScript provide all the functionality of a Stack, let us implement our own Stack class. Our class will have the following functions. push(element) − Function to push elements on top of the stack. pop() − Function that removes an element from the top and returns it. peek() − Returns the element on ... Read More

Stack Data Structure in Javascript

Sai Teja Kotha
Updated on 08-Dec-2022 12:00:27

835 Views

In this article, we are going to discuss the stack data structure in JavaScript. A stack is an Abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example – a deck of cards or a pile of plates, etc. A stack allows operations at one end only. This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which is placed (inserted or added) last, is accessed first. In stack terminology, insertion operation is called PUSH operation and removal operation is called POP ... Read More

Using iterator functions in Javascript

Sai Teja Kotha
Updated on 16-Dec-2022 15:52:34

309 Views

In this article, we are going to discuss the importance of iterator functions in JavaScript. We can use the iterator function to iterate over an array. Other than Explicit iteration, Javascript provides a variety of iteration functions that you can use to iterate over arrays. Here, we use the forEach(), the filter method(), and the map() method to iterate over the arrays and perform the required operations. ForEach Function This function takes a function as a parameter and executes it as you pass to it for every object in the array.Syntax Following is the syntax of the forEach() method. array.forEach(function(currentValue, ... Read More

The do…while loop in Javascript

Samual Sam
Updated on 15-Jun-2020 08:50:59

336 Views

The do...while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.For example,Examplelet i = 0; do {    console.log("Hello");    i = i + 1; } while (i < 5);This will give the output −OutputHello Hello Hello Hello Hello

Advertisements