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.

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]);
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);
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);
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"));
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));
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());
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());
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.