Create Stack Data Structure Using JavaScript Array Method

I already have an article about it. Click here to see it.

I am not going to details of it but the code implementation.

Stack is a linear data structure. It follows the principle of Last In First Out, and which is also known as LIFO.

There are mainly two operations in Stack. One is push() and another is pop(). The first one appends data to the last in Stack, and the second one removes the last added data.

const stack = []

// Push
stack.push(1)
stack.push(2)
stack.push(4)
stack.push(3)

console.log(stack) // [ 1, 2, 4, 3 ]


// Pop
stack.pop() // 3 is removed

// Peek
console.log(stack[stack.length-1]) // 4

console.log(stack) // [ 1, 2, 4 ]

We use another method to check the very last added data. It’s known as Peek.

This very basic data structure. We have already seen how easily we can create it using JS.

Scroll to Top