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

    JavaScript, Date

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

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

  • Luhn check

    JavaScript, Math

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

  • Returns the ISO format of the given number of seconds.

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

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

  • Arithmetic progression

    JavaScript, Math

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

  • Digitize number

    JavaScript, Math

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

  • Euclidean distance

    JavaScript, Math

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

  • Hex to RGB

    JavaScript, String

    Converts a color code to an rgb() or rgba() string if alpha value is provided.

  • Powerset

    JavaScript, Math

    Returns the powerset of a given array of numbers.

  • Sum of powers in range

    JavaScript, Math

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

  • Mapped array average

    JavaScript, Math

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

  • Cartesian product

    JavaScript, Math

    Calculates the cartesian product of two arrays.

  • Greatest common divisor

    JavaScript, Math

    Calculates the greatest common divisor between two or more numbers/arrays.