Table of contents
1.
Introduction
2.
How to Start Learning Data Structures with JavaScript?
2.1.
1. Learn About Complexities
2.2.
2. Learn Data Structures
2.2.1.
2.1 Array in JavaScript
2.2.2.
2.2 String in JavaScript
2.2.3.
2.3 Linked List in JavaScript
2.2.4.
2.4 Searching Algorithms
2.2.5.
2.5 Sorting Algorithm
2.2.6.
2.6 Hash
2.2.7.
2.7 Two Pointer
2.2.8.
2.8 Recursion
2.2.9.
2.9 Stack in JavaScript
2.2.10.
2.10 Queue in JavaScript
2.2.11.
2.11 Tree in JavaScript
2.2.12.
2.12 Priority Queue in JavaScript
2.2.13.
2.13 Map in JavaScript
2.2.14.
2.14 Set in JavaScript
2.2.15.
2.15 Graph in JavaScript
3.
3. Built-in Data Structures in JavaScript
4.
4. Practice Problems on DSA
5.
Frequently Asked Questions
5.1.
Which data structure is the fastest in JavaScript? 
5.2.
Is recursion better than iteration? 
5.3.
What is the difference between Stack and Queue? 
6.
Conclusion
Last Updated: Feb 19, 2025
Medium

Data Structures in JavaScript

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Data structures in JavaScript help in organizing and managing data efficiently. JavaScript provides both built-in and user-defined data structures to store and manipulate data, such as arrays, objects, sets, maps, stacks, and queues. Choosing the right data structure improves performance and optimizes memory usage.

Data Structures in JavaScript

In this article, you will learn about different data structures in JavaScript, their usage, advantages, and best practices to handle data effectively in programming.

How to Start Learning Data Structures with JavaScript?

Before diving into data structures, it is crucial to understand the basics of time and space complexities. This knowledge helps in choosing the right data structure for a given problem.

1. Learn About Complexities

Time complexity refers to how fast an algorithm runs, while space complexity refers to the memory usage of an algorithm. The Big O notation is commonly used to express these complexities. For example:

  • O(1) - Constant time (Fastest)
     
  • O(log n) - Logarithmic time
     
  • O(n) - Linear time
     
  • O(n^2) - Quadratic time (Slower)
     

Understanding complexities helps in writing efficient programs.

2. Learn Data Structures

Now, let's discuss some fundamental data structures in JavaScript.

2.1 Array in JavaScript

Arrays are used to store multiple values in a single variable.

let arr = [10, 20, 30, 40];
console.log(arr[2]); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

30

 

Arrays allow operations like sorting, searching, and filtering data efficiently.

2.2 String in JavaScript

Strings are sequences of characters used to store text.

let str = "Hello, World!";
console.log(str.length);
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

13

2.3 Linked List in JavaScript

A linked list consists of nodes where each node contains data and a reference to the next node.

class Node {
    constructor(value) {
        this.value = value;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = null;
    }
    append(value) {
        let newNode = new Node(value);
        if (!this.head) {
            this.head = newNode;
            return;
        }
        let temp = this.head;
        while (temp.next) {
            temp = temp.next;
        }
        temp.next = newNode;
    }
}

let list = new LinkedList();
list.append(10);
list.append(20);
console.log(list.head.value); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

10

2.4 Searching Algorithms

Searching algorithms help in finding elements in data structures.

Binary Search (O(log n)):

function binarySearch(arr, target) {
    let left = 0, right = arr.length - 1;
    while (left <= right) {
        let mid = Math.floor((left + right) / 2);
        if (arr[mid] === target) return mid;
        else if (arr[mid] < target) left = mid + 1;
        else right = mid - 1;
    }
    return -1;
}

2.5 Sorting Algorithm

Sorting algorithms arrange elements in a specific order.

Bubble Sort:

function bubbleSort(arr) {
    let n = arr.length;
    for (let i = 0; i < n - 1; i++) {
        for (let j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
            }
        }
    }
    return arr;
}

2.6 Hash

A hash table stores key-value pairs for fast lookups.

let hashMap = new Map();
hashMap.set("name", "Alice");
console.log(hashMap.get("name")); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

Alice

2.7 Two Pointer

The two-pointer technique helps solve array problems efficiently.

function twoSum(arr, target) {
    let left = 0, right = arr.length - 1;
    while (left < right) {
        let sum = arr[left] + arr[right];
        if (sum === target) return [left, right];
        else if (sum < target) left++;
        else right--;
    }
    return [];
}

2.8 Recursion

Recursion is a technique where a function calls itself.

function factorial(n) {
    if (n === 0) return 1;
    return n * factorial(n - 1);
}
console.log(factorial(5)); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

120

2.9 Stack in JavaScript

A stack follows the LIFO principle.

let stack = [];
stack.push(10);
stack.push(20);
console.log(stack.pop()); 
You can also try this code with Online Javascript Compiler
Run Code

 

Output: 

20

2.10 Queue in JavaScript

A queue follows the FIFO principle.

let queue = [];
queue.push(10);
queue.push(20);
console.log(queue.shift()); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

10

2.11 Tree in JavaScript

A tree is a hierarchical data structure.

class TreeNode {
    constructor(value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
}

2.12 Priority Queue in JavaScript

A priority queue assigns priorities to elements.

2.13 Map in JavaScript

A Map is a key-value pair structure.

2.14 Set in JavaScript

A Set stores unique elements.

2.15 Graph in JavaScript

A graph is a collection of nodes and edges.

3. Built-in Data Structures in JavaScript

JavaScript provides built-in data structures like arrays, objects, sets, and maps to manage data efficiently.

4. Practice Problems on DSA

  • Implement a linked list.
     
  • Sort an array using different sorting algorithms.
     
  • Solve common graph traversal problems.

Frequently Asked Questions

Which data structure is the fastest in JavaScript? 

Arrays and hash maps are the fastest for most operations due to their direct indexing.

Is recursion better than iteration? 

Recursion simplifies code but can be less efficient due to function call overhead.

What is the difference between Stack and Queue? 

A stack follows LIFO, whereas a queue follows FIFO.

Conclusion

In this article, we discussed data structures in JavaScript, including arrays, objects, sets, maps, stacks, queues, and linked lists. These structures help in organizing and managing data efficiently for various applications. Understanding JavaScript data structures allows developers to write optimized code, improve performance, and handle complex data operations effectively.

Live masterclass