JavaScript push / pop stack

There is no stack in JavaScript, but there is an array that can be used as a stack. At the same time, by manipulating the JavaScript push / pop array methods , you can have at your disposal a stack, an array, and your own data organization.

javascript push

As a first approximation, arrays are a familiar and popular data structure. But working with them as a stack gives them capabilities not provided by the syntax of the language. Adding / removing via JavaScript push / pop to the end or unshift / shift to the beginning is not only convenient, but also practical.

Using methods

The array can be replenished with new elements using the push method. The result of this method will be a new number of array elements. The reverse procedure - the pop method has no parameters, but returns the last element of the array as the result.

javascript push object

As follows from the syntax and logic of the language, arrays can work with data of any type.

JavaScript push object - nonsense or progress?

The browser language is not inferior to its more “free” colleagues in relation to object-oriented programming, that is, it also makes it possible to create objects. At the same time, there is no keyword denoting something related to OOP.

Generally speaking, what is available in JavaScript has not yet allowed itself to have a single “browser-free” programming language. The most original thing - creating an object here is the work of the programmer, starting with the name of the object.

javascript pop push

JavaScript pop & push methods when using objects provide the programmer with the opportunity to create a multifunctional object in the direct meaning of the word.

For example, having several interconnected but different pages (objects that are not connected in any way with the logic of the dialogue), you can implement the movement of a visitor through them. Having placed the object of the initial page on the stack (array) using the push method (the visitor came), give him a choice of further actions.

javascript push object

The next push will place on top the page object that the visitor has selected. Rollback pop will bring it back. Moving on is another push, and so the dialogue of the current visitor will be formed. This can be useful to the developer in the sense of experience and statistics, and provide navigation in the current session of the site.

Stack, array and data organization

There are many tasks when the result requires a multivariate choice. If you select a set of if or case statements for the implementation, you get a large, long and branchy “bush” of conditions.

javascript pop push

In general, this is not a bad decision, but when you need to change something, you will have to remember for a long time which condition follows, and the algorithm will be illegible, and the most unpleasant, can become a source of difficult to detect errors.

Using the stack in almost all cases can be done easier.

There is a task: you need to choose an artist from hundreds available. Each performer can do something from three positions (from one to three in any combination):

  • t - do maintenance;
  • s - can fully perform repair work;
  • i - has the right to make warranty repairs.

To quickly select the contractor for the order with the desired type (types of work), you can do three JavaScript push operations and merge the array into one line.

javascript pop push

Search by line in line is always more visual than numerous conditions. This is a simple case of only three by three options, but even here there will be much more code than in one comparison of just two lines.

Source: https://habr.com/ru/post/C6835/


All Articles