7 JavaScript Data Structures you must know

Emma Delaney
3 min readDec 22, 2023

--

Data structures are one of the most tested topics for many companies. They form the basis of the IT industry and are widely used in artificial intelligence, operation of computer systems, graphics, etc.

In this blog post I will cover the seven most commonly used JavaScript data structures that every JS developer should know.

1. Arrays:

Arrays are the simplest and most commonly used data structure in JavaScript. These are collections of items stored in contiguous locations. JavaScript arrays are dynamic and allow you to easily add or remove elements. They are indexed by 0 and support several built-in manipulation methods.

// Example of an array
let fruits = ['apple', 'banana', 'orange'];
fruits.push('grape'); // Adds 'grape' to the end of the array

2. Objects:

Objects are key-value pairs and are used to represent and store data. They are very flexible and can contain different types of data, including other objects. Objects are often used to model real-world entities and their properties.

// Example of an object
let person = {
name: 'John',
age: 30,
city: 'New York'
};
console.log(person.name); // Accessing the value using the key

3. Linked Lists:

Linked lists are composed of nodes, where each node contains data and a reference to the next node in the sequence. Unlike arrays, linked lists provide dynamic memory allocation, making them suitable for scenarios where the size of the data structure may change frequently.

// Example of a linked list node
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}

4. Stacks:

// Example of a stack using an array
let stack = [];
stack.push('a'); // Pushing an element onto the stack
let topElement = stack.pop(); // Popping the top element from the stack

A stack is a LIFO (last in, first out) data structure in which elements are added and removed at the same end, called the top. Stacks are commonly used to handle function calls, deletion mechanisms, and expression parsing.

5. Queues:

// Example of a queue using an array
let queue = [];
queue.push('a'); // Enqueue an element
let frontElement = queue.shift(); // Dequeue the front element

A queue is a First In, First Out (FIFO) data structure where elements are added at the rear and removed from the front. Queues are essential in scenarios like task scheduling, breadth-first search, and print job management.

6. Hash Tables:

// Example of a simple hash table
let hashTable = {};
hashTable['name'] = 'Alice';
let value = hashTable['name']; // Retrieving value using the key

Hash tables use a hash function to map keys to indexes, allowing for efficient data retrieval. They are often used to implement arrays, dictionaries, and associative caches. JavaScript objects can be thought of as a form of hash table.

7. Trees:

Trees are hierarchical data structures with a root node and branches leading to leaf nodes. Binary trees, in particular, are often used in applications such as heaps and binary search trees.

// Example of a binary tree node
class TreeNode {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}

Mastering these basic JavaScript data structures is essential for writing efficient, scalable code. Depending on the problem you encounter, choosing the correct data structure can have a significant impact on the performance of your applications. By understanding how these data structures work and when to use them, you will be able to design robust and optimized JavaScript applications.

--

--