Algorithms and Data Structures in JavaScript: A Practical Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Algorithms and Data Structures in JavaScript: In Practice



In modern web development, knowledge of algorithms and data structures is not just an academic skill, but a tool for creating fast and scalable applications. JavaScript, especially in the Node.js environment, allows implementing both simple and complex algorithmic constructs. In this article, we will break down key data structures and algorithms in practice, with code examples that can be used immediately in projects.



1. Basic Data Structures: Stack, Queue, and Linked List



Let's start with the fundamentals. Stack and queue are linear structures often used in parsing, task queues, and action history management.



Stack (LIFO)

A stack works on the "last in, first out" principle. Let's implement it based on an array:



class Stack {  constructor() {    this.items = [];  }

push(element) { this.items.push(element); }

pop() { return this.items.pop(); }

peek() { return this.items[this.items.length - 1]; }

isEmpty() { return this.items.length === 0; }}

// Usage exampleconst stack = new Stack();stack.push('Page 1');stack.push('Page 2');console.log(stack.pop()); // 'Page 2'


Queue (FIFO)

A queue is useful for processing tasks in order of arrival. Implementation using a class:



class Queue {  constructor() {    this.items = {};    this.front = 0;    this.rear = 0;  }

enqueue(element) { this.items[this.rear] = element; this.rear++; }

dequeue() { const item = this.items[this.front]; delete this.items[this.front]; this.front++; return item; }

peek() { return this.items[this.front]; }

size() { return this.rear - this.front; }}

// Exampleconst queue = new Queue();queue.enqueue('Task 1');queue.enqueue('Task 2');console.log(queue.dequeue()); // 'Task 1'


Linked List

A linked list is efficient for inserting and deleting elements in the middle of the list. Let's implement a singly linked list:



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

class LinkedList { constructor() { this.head = null; this.tail = null; this.length = 0; }

append(value) { const newNode = new Node(value); if (!this.head) { this.head = newNode; this.tail = newNode; } else { this.tail.next = newNode; this.tail = newNode; } this.length++; }

prepend(value) { const newNode = new Node(value); newNode.next = this.head; this.head = newNode; if (!this.tail) { this.tail = newNode; } this.length++; }

print() { const arr = []; let current = this.head; while (current) { arr.push(current.value); current = current.next; } return arr; }}

// Exampleconst list = new LinkedList();list.append(10);list.append(20);list.prepend(5);console.log(list.print()); // [5, 10, 20]


2. Search Algorithms: Linear and Binary



Searching is one of the most common operations. Let's look at two approaches: simple linear search and efficient binary search.



Linear Search

Suitable for unsorted data. Complexity O(n):



function linearSearch(arr, target) {  for (let i = 0; i < arr.length; i++) {    if (arr[i] === target) {      return i; // Return the index    }  }  return -1;}

console.log(linearSearch([3, 7, 1, 9, 4], 9)); // 3


Binary Search

Requires a sorted array. Complexity O(log n). Implementation in Node.js:



function binarySearch(arr, target) {  let left = 0;  let right = arr.length - 1;

while (left <= right) { const mid = M

Blogs

Book Recommendations