Push operation on stack in JavaScript

1. Overview

The push operation on a stack is used to add an element to the top of the stack. This operation is typically used when we need to work with data in a LIFO (Last In First Out) form. The push operation adds the element to the end of the array, which makes it the last element in the stack.

2. How to Push data on stack in JavaScript

  1. It creates a new stack.
  2. It pushes two numbers onto the stack.
  3. It checks that the stack contains the numbers in the correct order.

3. Push data on stack in JavaScript

JS

The push() method adds one or more elements to the end of an array and returns the new length of the array.

The push() method can take multiple arguments, each of which will be appended to the end of the array in the order they are passed in.

This example will add two items to the end of an array:
var arr = [1,2,3];
arr.push(4,5); // [1,2,3,4,5]

4. Conclusion

Push data on stack benefits helps in reversing any set of data or strings. ‘

Application of Stack in real life: CD/DVD stand.

Stack of books in a book shop.

padmaraj
Latest posts by padmaraj (see all)

Leave a Comment