Javascript Articles

Page 396 of 534

Multi Dimensional Arrays in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 394 Views

Multi-dimensional arrays in JavaScript are arrays that contain other arrays as elements. They're useful when you need to organize data in rows and columns, like storing temperatures for each day of the week at different time intervals. Creating Multi-Dimensional Arrays Instead of creating separate arrays for each day: let monday = [35, 28, 29, 31]; let tuesday = [33, 24, 25, 29]; console.log("Monday temperatures:", monday); console.log("Tuesday temperatures:", tuesday); Monday temperatures: [ 35, 28, 29, 31 ] Tuesday temperatures: [ 33, 24, 25, 29 ] You can use a multi-dimensional array to ...

Read More

Clearing the elements of a Stack in Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 1K+ Views

Consider a simple stack class in JavaScript. We'll implement a clear method to remove all elements from the stack. Basic Stack Implementation class Stack { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; ...

Read More

Creating a Queue in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 787 Views

Though Arrays in JavaScript provide all the functionality of a Queue, let us implement our own Queue class. Our class will have the following functions − enqueue(element): Function to add an element in the queue. dequeue(): Function that removes an element from the queue. peek(): Returns the element from the front of the queue. isFull(): Checks if we reached the element limit on the queue. isEmpty(): checks if the queue is empty. clear(): Remove all elements. display(): display all contents of the array Let's start by defining a simple class with a constructor that takes the ...

Read More

Remove elements from a queue using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

Dequeuing elements from a Queue means removing them from the front/head of the queue. We are taking the start of the container array to be the head of the queue as we'll perform all operations with respect to it. 10 20 30 40 FRONT REAR ...

Read More

Creating a Priority Queue using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 569 Views

A priority queue is a data structure where each element has a priority. Elements are removed based on their priority rather than insertion order. Here's how to implement one using JavaScript arrays. Priority Queue Structure Our priority queue class will include these essential methods: enqueue(element, priority): Add an element with a specific priority dequeue(): Remove and return the highest priority element peek(): Return the highest priority element without removing it isEmpty(): Check if the queue is empty isFull(): Check if the queue has reached capacity display(): Show all queue contents Basic Implementation Let's ...

Read More

Add elements to a PriorityQueue using Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 268 Views

Enqueuing elements to a PriorityQueue means adding them in the array in order of the priority of the element. We'll consider higher numbers to be higher priorities. We'll loop through the container till we find a lower priority and then add the element there. If not, then we'll push it at the end of the container. Note that we're creating the element object with the data and priority. Hence we can implement the enqueue function as follows: The enqueue() Method enqueue(data, priority) { // Check if Queue is full ...

Read More

Remove elements from a PriorityQueue using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 390 Views

Dequeuing elements from a PriorityQueue means removing the element with the highest priority. Since we store elements with the highest priority at the end of the array, we can simply pop it to dequeue it. Priority Queue Structure World Priority: 2 Hello Priority: 3 Foo Priority: 8 Highest Priority ...

Read More

Peeking elements from a PriorityQueue using JavaScript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 193 Views

Peeking a PriorityQueue means getting the element with the highest priority without removing it from the queue. This operation is useful when you need to inspect the next item to be processed without actually processing it. The peek() Method Implementation The peek function returns the element with the highest priority. In our implementation, elements are stored in ascending order of priority, so the last element has the highest priority: peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); ...

Read More

The PriorityQueue Class in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 271 Views

A Priority Queue is a data structure where each element has an associated priority. Elements with higher priority are served before elements with lower priority. In JavaScript, we can implement this using a class-based approach. Complete PriorityQueue Implementation Here's a complete implementation of the PriorityQueue class with all essential methods: class PriorityQueue { constructor(maxSize) { // Set default max size if not provided if (isNaN(maxSize)) { maxSize = 10; ...

Read More

Basic Operations supported by a list in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 244 Views

JavaScript arrays (lists) support several fundamental operations that allow you to manipulate and access data efficiently. Here are the core operations you can perform on arrays. Basic List Operations Insertion − Add elements at the beginning, middle, or end of the list Deletion − Remove elements from any position in the list Display − Show the complete list contents Search − Find elements using specific values or conditions Access − Retrieve elements by their index position Insertion Operations You ...

Read More
Showing 3951–3960 of 5,340 articles
« Prev 1 394 395 396 397 398 534 Next »
Advertisements