Web Development Articles

Page 398 of 801

Multi Dimensional Arrays in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 409 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

Creating a Set using Javascript

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

In JavaScript, a Set is a collection of unique values. You can create sets using the native ES6 Set class or implement a custom set class. Let's explore both approaches. Creating Sets with ES6 Set Class The simplest way to create a set is using the built-in Set constructor: // Create empty set const set1 = new Set(); // Create set with initial values const set2 = new Set([1, 2, 5, 6]); console.log(set1); console.log(set2); Set(0) {} Set(4) { 1, 2, 5, 6 } Custom Set Implementation You can ...

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

Clearing the set using Javascript

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

In JavaScript, the clear() method removes all elements from a Set. For built-in Sets, you can use the native clear() method. For custom Set implementations, you reassign the container to an empty object. Using Built-in Set clear() Method const mySet = new Set(); mySet.add(1); mySet.add(2); mySet.add(5); console.log("Before clear:", mySet); mySet.clear(); console.log("After clear:", mySet); console.log("Size:", mySet.size); Before clear: Set(3) { 1, 2, 5 } After clear: Set(0) {} Size: 0 Custom Set Implementation For a custom Set class, the clear method reassigns the container to a new empty object: ...

Read More

Creating a Queue in Javascript

Samual Sam
Samual Sam
Updated on 15-Mar-2026 794 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 574 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 272 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

Loop through a Set using Javascript

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

In JavaScript, you can loop through a Set using several methods. The most common approaches are using the forEach() method, for...of loop, or converting to an array. Using forEach() Method The forEach() method executes a provided function for each value in the Set: const mySet = new Set([1, 2, 5, 8]); mySet.forEach(value => { console.log(`Element is ${value}`); }); Element is 1 Element is 2 Element is 5 Element is 8 Using for...of Loop The for...of loop provides a cleaner syntax for iterating over Set values: ...

Read More

Remove elements from a PriorityQueue using Javascript

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 394 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
Showing 3971–3980 of 8,010 articles
« Prev 1 396 397 398 399 400 801 Next »
Advertisements