Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Web Development Articles
Page 397 of 801
Joining two Arrays in Javascript
In JavaScript, there are multiple ways to join two arrays. The method you choose depends on whether you want to create a new array or modify an existing one. Using concat() Method (Creates New Array) The concat() method creates a new array without modifying the original arrays: let arr1 = [1, 2, 3, 4]; let arr2 = [5, 6, 7, 8]; let arr3 = arr1.concat(arr2); console.log("Original arr1:", arr1); console.log("Original arr2:", arr2); console.log("New combined array:", arr3); Original arr1: [1, 2, 3, 4] Original arr2: [5, 6, 7, 8] New combined array: [1, 2, ...
Read MoreArrays Data Structure in Javascript
The array is a container which can hold a fixed number of items and these items should be of the same type. It stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Why do we need arrays? Let's say you want to record the average temperatures of all days of the week. You can record them as follows − let avgTempMon = 35; let ...
Read MoreAdding an element at the end of the array in Javascript
In JavaScript, adding elements to the end of an array is a fundamental operation. The most common and efficient method is using the push() method, which modifies the original array by appending one or more elements. An array is a special variable that can hold multiple values in a single ordered collection. Arrays in JavaScript are dynamic, meaning you can add or remove elements after creation. What is an Array? An array is a collection of items stored at contiguous memory locations. Arrays allow random access to elements, making it faster to access elements by their position ...
Read MorePeeking elements from a Queue in Javascript
Peeking a Queue means getting the value at the head of the Queue without removing it. This allows you to inspect the next element that would be dequeued without modifying the queue structure. Syntax peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; } Complete Queue Implementation with Peek class Queue { constructor(maxSize) { ...
Read MoreAdding an element at the start of the array in Javascript
In JavaScript, adding an element at the start of an array is a common operation. The most straightforward method is using the unshift() method, which adds one or more elements to the beginning of an array and returns the new length. An array is a data structure that stores multiple values in a single variable. Arrays in JavaScript are zero-indexed, meaning the first element is at position 0. Syntax array.unshift(element1, element2, ..., elementN) Parameters The unshift() method accepts one or more parameters representing the elements to add at the beginning of the array. ...
Read MoreClearing the elements of the Queue in Javascript
We can clear all elements from a queue by reassigning the container to an empty array. This is the most efficient way to remove all queue elements at once. Syntax clear() { this.container = []; } Example Here's a complete example showing how the clear method works with a queue implementation: class Queue { constructor(maxSize = 10) { this.container = []; this.maxSize = maxSize; ...
Read MoreRemoving an element from the start of the array in javascript
In JavaScript, there are several methods to remove the first element from an array. The most common approaches are using the shift() method, slice() method, or the Underscore.js _.rest() method. Using shift() Method (Recommended) The shift() method removes and returns the first element from an array, modifying the original array. Syntax array.shift() Example Remove first element using shift() ...
Read MoreRemoving an element from a given position of the array in Javascript
In JavaScript, you can remove an element from a specific position in an array using several methods. Each approach has different behaviors and use cases. Syntax Here are the main syntaxes for removing elements: // Delete operator (leaves undefined) delete arr[index]; // Slice method (creates new array) arr.slice(0, index).concat(arr.slice(index + 1)); // Splice method (modifies original array) arr.splice(index, 1); Using delete Operator The delete operator removes an element but leaves undefined in its place, keeping the array length unchanged. Remove element using delete ...
Read MoreSearching an element in Javascript Array
JavaScript provides several methods to search for elements in arrays. Each method has its own use case depending on whether you're searching for primitive values or complex objects. Using indexOf() for Primitive Values The indexOf() method searches through the array and returns the index of the first matching element, or -1 if not found. let people = ["Harry", "Martha", "John", "Sam"]; console.log(people.indexOf("John")); console.log(people.indexOf("Jim")); 2 -1 Using find() for Complex Objects The find() method returns the first element that matches the condition provided in the callback function. let people ...
Read MoreClearing the elements of the PriorityQueue using Javascript
In JavaScript, clearing a PriorityQueue can be accomplished by resetting the container array that holds the queue elements. This is a simple but effective approach. Clear Method Implementation The most straightforward way to clear a PriorityQueue is to reassign the container to an empty array: clear() { this.container = []; } Complete Example Here's a working example demonstrating the clear functionality with a basic PriorityQueue implementation: class PriorityQueue { constructor() { this.container = []; ...
Read More