Skip to content

# JavaScript Algorithms

The JavaScript algorithms collection includes implementations and examples of popular algorithms in ES6. Algorithmic snippets are primarily a learning resource, as they might already be implemented natively or require optimizations to run in production.

• JavaScript ·

### K-means clustering

Groups the given data into `k` clusters, using the k-means clustering algorithm.

• JavaScript ·

### K-nearest neighbors

Classifies a data point relative to a labelled data set, using the k-nearest neighbors algorithm.

• JavaScript ·

### Heap sort

Sort an array of numbers, using the heapsort algorithm.

• JavaScript ·

### Implement the Caesar cipher in JavaScript

The Caesar cipher is a simple substitution cipher, which can be easily implemented with a few lines of JavaScript code.

• JavaScript ·

### Implementing the Luhn Algorithm in JavaScript

Implement the Luhn Algorithm, used to validate a variety of identification numbers.

• JavaScript ·

### Bucket sort

Sort an array of numbers, using the bucket sort algorithm.

• JavaScript ·

### Quick sort

Sort an array of numbers, using the quicksort algorithm.

• JavaScript ·

### Selection sort

Sort an array of numbers, using the selection sort algorithm.

• JavaScript ·

### Big-O Cheat Sheet

Learn everything you need to know about Big-O notation with this handy cheatsheet.

• JavaScript ·

### Check if a number is prime in JavaScript

Learn how to check if a number is prime and how to generate prime numbers up to a given number in JavaScript.

• JavaScript ·

### Calculate the Euclidean distance in JavaScript

Use JavaScript's `Math.hypot()` to calculate the Euclidean distance between two points.

• JavaScript ·

### Calculate the Levenshtein distance between two strings in JavaScript

Implement the Levenshtein distance algorithm in JavaScript to calculate the difference between two strings.

• JavaScript ·

### Merge sort

Sort an array of numbers, using the merge sort algorithm.

• JavaScript ·

### Arithmetic and geometric progression in JavaScript

Create arrays of numbers in arithmetic and geometric progression.

• JavaScript ·

### Count substrings of string

Counts the occurrences of a substring in a given string.

• JavaScript ·

### Bubble sort

Sort an array of numbers, using the bubble sort algorithm.

• JavaScript ·

### Insertion sort

Sort an array of numbers, using the insertion sort algorithm.

• JavaScript ·

### How can I calculate the greatest common divisor & least common multiple in JavaScript?

Use JavaScript to calculate the greatest common divisor and least common multiple of two or more numbers.

• JavaScript ·

### Maximum subarray

Finds a contiguous subarray with the largest sum within an array of numbers.

• JavaScript ·

### Binary search

Finds the index of a given element in a sorted array using the binary search algorithm.

• JavaScript ·

### Fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

• JavaScript ·

### Linear search

Finds the first index of a given element in an array using the linear search algorithm.

• JavaScript ·

### Find the prime factors of a number in JavaScript

Find the prime factors of a number using trial division in JavaScript.

• JavaScript ·

### Vector distance

Calculates the distance between two vectors.

Start typing a keyphrase to see matching snippets.