View : 0

12/04/2026 18:17pm

JS2GO EP.31 Using Queues and Stacks in JavaScript and Go

JS2GO EP.31 Using Queues and Stacks in JavaScript and Go

#Stacks

#Queues

#JavaScript

#Go

Queues and Stacks are linear data structures that manage data in a specific order, essential for task management, recursion, undo/redo operations, or queue systems in real-world projects. In this article, we’ll explore how to create and use Queues and Stacks in JavaScript and Go, including practical examples and best practices.

 

1. Stack

 

A Stack is a data structure following LIFO (Last In, First Out) principle.

 

JavaScript: Stack using Array

const stack = [];

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

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

// Pop
const top = stack.pop();
console.log(top);   // 3
console.log(stack); // [1,2]

 

Best Practices in JavaScript:
✔️ Use for undo/redo, function calls, recursion
✔️ JS arrays can be used directly with push/pop

 

Go: Stack using Slice

package main

import "fmt"

func main() {
    stack := []int{}

    // Push
    stack = append(stack, 1)
    stack = append(stack, 2)
    stack = append(stack, 3)
    fmt.Println(stack) // [1 2 3]

    // Pop
    top := stack[len(stack)-1]
    stack = stack[:len(stack)-1]
    fmt.Println(top)   // 3
    fmt.Println(stack) // [1 2]
}

 

Best Practices in Go:
✔️ Slice can append/pop easily
⚠️ Be careful with index out-of-range errors

 

2. Queue

 

A Queue is a data structure following FIFO (First In, First Out) principle.

 

JavaScript: Queue using Array

const queue = [];

// Enqueue
queue.push(1);
queue.push(2);
queue.push(3);

// Dequeue
const first = queue.shift();
console.log(first); // 1
console.log(queue); // [2,3]

 

Best Practices in JavaScript:
✔️ Use for task scheduling, event queue
⚠️ shift() has performance cost for large arrays → consider linked list or deque for production

 

Go: Queue using Slice

package main

import "fmt"

func main() {
    queue := []int{}

    // Enqueue
    queue = append(queue, 1)
    queue = append(queue, 2)
    queue = append(queue, 3)

    // Dequeue
    first := queue[0]
    queue = queue[1:]
    fmt.Println(first) // 1
    fmt.Println(queue) // [2 3]
}

 

Best Practices in Go:
✔️ Slice can append/dequeue easily
⚠️ For large queues, consider container/list for efficiency

 

3. Real-World Usage of Queues and Stacks

 

✔️ Undo/Redo: Use stack to store previous states
✔️ Task Scheduling: Use queue for background tasks
✔️ DFS/BFS Algorithms: Stack/Queue are core structures
✔️ Concurrent Queue (Go): Use channel for concurrent operations

 

Example in Go: Concurrent Queue with Channel

ch := make(chan int, 5)
go func() {
    for i := 1; i <= 5; i++ {
        ch <- i // Enqueue
    }
    close(ch)
}()

for val := range ch {
    fmt.Println(val) // Dequeue
}

 

4. Comparison: JavaScript vs Go

 

FeatureJavaScriptGo
StackArray push()/pop()Slice append/pop
QueueArray push()/shift()Slice append/dequeue or channel
Type Safety❌ No✔️ Yes
Concurrent QueueManual / libraryChannel (built-in)
Use CasesUndo/Redo, recursionTask scheduling, concurrency

 

Recommendations:
✔️ Frontend / lightweight tasks → JavaScript Array stack/queue
✔️ Backend / concurrent tasks → Go slice stack/queue or channel

 


 

Next Episode

 

In EP.32 of the JS2GO series, we will explore Linked Lists in Go vs JavaScript to understand dynamic node management and apply it to complex data structures.

 

Read more

🔵 Facebook: Superdev Academy

🔴 YouTube: Superdev Academy

📸 Instagram: Superdev Academy

🎬 TikTok: https://www.tiktok.com/@superdevacademy?lang=th-TH

🌐 Website: https://www.superdevacademy.com/en