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.

  • formatDuration

    JavaScript, Date

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

  • luhnCheck

    JavaScript, Math

    Implements the Luhn Algorithm used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc.

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

  • formatSeconds

    JavaScript, Date

    Returns the ISO format of the given number of seconds.

  • primes

    JavaScript, Math

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

  • euclideanDistance

    JavaScript, Math

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

  • sumPower

    JavaScript, Math

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

  • arithmeticProgression

    JavaScript, Math

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

  • averageBy

    JavaScript, Math

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

  • digitize

    JavaScript, Math

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

  • gcd

    JavaScript, Math

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

  • hexToRGB

    JavaScript, String

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

  • sumBy

    JavaScript, Math

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

  • geometricProgression

    JavaScript, Math

    Initializes an array containing the numbers in the specified range where start and end are inclusive and the ratio between two terms is step. Returns an error if step equals 1.

  • powerset

    JavaScript, Math

    Returns the powerset of a given array of numbers.