# freeCodeCamp Challenge Guide: Learn how a Stack Works

**URL:** <https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-learn-how-a-stack-works/301705>\
**Category:** freeCodeCamp Contributors\
**Tags:** guide\
**Created:** [August 3, 2019, 5:38pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-learn-how-a-stack-works/301705 "2019-08-03T17:38:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![camperbot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/camperbot/32/18364_2.png) [@camperbot](https://forum.freecodecamp.org/u/camperbot)\
**Post date:** [August 3, 2019, 5:38pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-learn-how-a-stack-works/301705/1 "2019-08-03T17:38:08Z")

</div>

# [Learn how a Stack Works](https://www.freecodecamp.org/learn/coding-interview-prep/data-structures/learn-how-a-stack-works)

* * *

## Problem Explanation

- Stacks are an abstract data structures.
- They follow LIFO (Last In First Out) or FILO (First In Last Out) principle.
- Stack’s insertion and deletion operations are of **O(1)** time complexity.
- In JavaScript, arrays can be treated as a Stack since `.push()` and `.pop()` methods have time complexity of **O(1)**.
- In this challenge we need to `.pop()` and then `.push()` into the stack.

* * *

## Solutions

> **Solution 1 (Click to Show/Hide)**
>
> ```js
> var homeworkStack = ["BIO12", "HIS80", "MAT122", "PSY44"];
> 
> homeworkStack.pop();
> homeworkStack.push("CS50");
> 
> ```
> 
> #### Relevant Links
> 
> - [Wikipedia](https://en.wikipedia.org/wiki/Stack_(abstract_data_type))
> - Video by [Hackerrank](https://www.youtube.com/watch?v=wjI1WNcIntg)

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 8, 2025, 3:51pm UTC](https://forum.freecodecamp.org/t/freecodecamp-challenge-guide-learn-how-a-stack-works/301705/2 "2025-01-08T15:51:14Z")

</div>

Closed on January 8, 2025, 3:51pm UTC


