Start of main content## 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.

### Format duration

Returns the human-readable format of the given number of milliseconds.

JavaScript, Date · Oct 22, 2020

### Luhn check

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

JavaScript, Math · Jan 30, 2022

### Tip: Min and max value in a JavaScript array

When working with numeric arrays in JavaScript, you might find yourself in need of finding the minimum or maximum value. Here's a quick and easy way to do it.

JavaScript, Array · Nov 6, 2021

### Number of seconds to ISO format

Returns the ISO format of the given number of seconds.

JavaScript, Date · Oct 13, 2021

### Primes up to given number

Generates primes up to a given number, using the Sieve of Eratosthenes.

JavaScript, Math · Dec 28, 2020

### Digitize number

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

JavaScript, Math · Oct 18, 2020

### Euclidean distance

Calculates the distance between two points in any number of dimensions.

JavaScript, Math · Dec 28, 2020

### Hex to RGB

Converts a color code to an

`rgb()`

or`rgba()`

string if alpha value is provided.JavaScript, String · Oct 19, 2020

### Maximum subarray

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

JavaScript, Algorithm · Sep 7, 2022

### Sum of powers in range

Calculates the sum of the powers of all the numbers from

`start`

to`end`

(both inclusive).JavaScript, Math · Oct 22, 2020

### Arithmetic progression

Creates an array of numbers in the arithmetic progression, starting with the given positive integer and up to the specified limit.

JavaScript, Math · Oct 13, 2021

### Mapped array average

Calculates the average of an array, after mapping each element to a value using the provided function.

JavaScript, Math · Oct 21, 2020