Found 8894 Articles for Front End Technology

Popping elements from a Stack in Javascript

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

211 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

191 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

686 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

233 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

184 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

Multi Dimensional Arrays in Javascript

Samual Sam
Updated on 15-Jun-2020 07:59:56

231 Views

Basically, multi-dimension arrays are used if you want to put arrays inside an array. Let's take an example. Say you wanted to store every 6 hour's temperature for every weekday. You could do something like −let monday = [35, 28, 29, 31]; let tuesday = [33, 24, 25, 29]; //...This is a good place to use a multidimensional array instead. A multidimensional array is nothing but an array of arrays. If we take forward our example, each row will represent a day while each entry in the row will represent a temp entry. For example, let temps = [   ... Read More

Searching an element in Javascript Array

Sharon Christine
Updated on 15-Jun-2020 08:05:09

382 Views

Javascript provides a collection of functions that you can use to find elements in an array. Let's start with the most basic one. The indexOf function goes through the entire array and returns the index of the element you searched for, if it is found else it returns -1. For example, Examplelet people = ["Harry", "Martha", "John", "Sam"]; console.log(people.indexOf("John")) console.log(people.indexOf("Jim"))OutputThis will give the output −2 -1There are other, more complex functions that you can use to make search more powerful. Let's look at find() method. The find() method returns the first object matching the condition you provide it as the ... Read More

Joining two Arrays in Javascript

Sharon Christine
Updated on 15-Jun-2020 08:12:22

167 Views

There are two ways to join 2 arrays in Javascript. If you want to get a new array and not want to disturb the existing arrays while joining the two arrays then you should use the concat method as follows − Examplelet arr1 = [1, 2, 3, 4]; let arr2 = [5, 6, 7, 8]; let arr3 = arr1.concat(arr2); console.log(arr1); console.log(arr2); console.log(arr3);OutputThis will give the output −[1, 2, 3, 4] [5, 6, 7, 8] [1, 2, 3, 4, 5, 6, 7, 8]Note that the existing arrays were not modified. If you want to join in place, you'll need to use the ... Read More

Removing an element from a given position of the array in Javascript

Sai Teja Kotha
Updated on 07-Dec-2022 13:12:21

189 Views

In this article, we are going to discuss removing an element from a given position of the array in JavaScript. The delete operator, the slice() method, and the splice() method can be used to remove an element from a given position of the array in this article. Following is the syntax of the delete operator. let del = delete arr[index]; Following is the syntax of the slice() method. arr.slice(start, end) Following is the syntax of the splice() method. arr.splice(position, no_of_elem) Using delete Operator Example In this example, we are going to discuss the use of the ... Read More

Advertisements