# JavaScript Math Snippets

The JavaScript snippet collection contains a wide variety of ES6 helper functions. It includes helpers for dealing with primitives, arrays and objects, as well as algorithms, DOM manipulation functions and Node.js utilities.

• JavaScript ·

### Formatting numeric values in JavaScript

Learn common number formatting operations, such as rounding, padding, optional decimal marks, currency, seconds, bytes, and more.

• JavaScript ·

### Modeling money, currencies & exchange rates using JavaScript

A deep dive into modeling money, currencies, and exchange rates using JavaScript.

• JavaScript ·

### How can I convert between hexadecimal, RGB, HSL and HSB color formats in JavaScript?

Learn how to easily convert between the various color formats, using JavaScript and a few simple formulas.

• JavaScript ·

### Calculate the ranking of a JavaScript array

Calculate the ranking of an array based on a comparator function in JavaScript.

• JavaScript ·

### Implementing the Luhn Algorithm in JavaScript

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

• JavaScript ·

### Min and max value of a JavaScript array

When working with JavaScript arrays, you might need the minimum or maximum value. Here are a few quick and easy ways to do it.

• JavaScript ·

### Implement the SDBM hash function in JavaScript

SDBM is a simple, non-cryptographic hash function that can hash strings into whole numbers. Here's a JavaScript implementation.

• JavaScript ·

### Convert an integer to a roman numeral in JavaScript

Roman numerals are often used for stylistic reasons, but converting an integer to a roman numeral can be a bit tricky.

• JavaScript ·

### Calculate the Euclidean distance in JavaScript

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

• JavaScript ·

### How can I find the insertion index of an element in a sorted JavaScript array?

Given a sorted array, find the correct index to insert a given value.

• 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 ·

### Create an array of partial sums in JavaScript

Create an array of partial sums, using `Array.prototype.reduce()` and `Array.prototype.slice()`.

• JavaScript ·

### Create a unit converter data structure in JavaScript

Learn how to create a unit converter data structure in JavaScript that can convert between any compatible units.

• JavaScript ·

### Perform math operations on arrays of numbers in JavaScript

Learn how to work with arrays of numbers in JavaScript, performing common math operations such as sum, average, product and more.

• JavaScript ·

### Arithmetic and geometric progression in JavaScript

Create arrays of numbers in arithmetic and geometric progression.

• JavaScript ·

### Digitize number

Converts a number to an array of digits, removing its sign if necessary.

• 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 ·

### Calculate the powerset of a JavaScript array

Generate the powerset of a given array of numbers or other primitive values.

• JavaScript ·

### Cartesian product of two JavaScript arrays

Create a new array out of the two supplied by creating each possible pair from the arrays.

• JavaScript ·

### Maximum subarray

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

• JavaScript ·

### Sum of powers in range

Calculates the sum of the powers of all the numbers from `start` to `end` (both inclusive).

• JavaScript ·

### How can I remove trailing zeros from a number in JavaScript?

When formatting decimal values in JavaScript, trailing zeros can be undesired. Here's how to deal with them.

• JavaScript ·

### Midpoint of two points

Calculate the midpoint between two pairs of points in a 2D plane, and beyond.

• JavaScript ·

### Generate a random hex color code in JavaScript

Learn how to generate a random hexadecimal color code with a few lines of JavaScript.

Start typing a keyphrase to see matching snippets.