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.

  • Product of numeric values

    Calculates the product of two or more numbers/arrays.

    JavaScript, Math · Oct 22, 2020

  • Array sum

    Calculates the sum of two or more numbers/arrays.

    JavaScript, Math · Oct 22, 2020

  • Insertion index in sorted array

    Finds the lowest index at which a value should be inserted into an array in order to maintain its sorting order.

    JavaScript, Array · Oct 22, 2020

  • Number to fixed-point notation without trailing zeros

    Formats a number using fixed-point notation, if it has decimals.

    JavaScript, Math · May 10, 2022

  • Random integer in range

    Generates a random integer in the specified range.

    JavaScript, Math · Oct 22, 2020

  • Random number in range

    Generates a random number in the specified range.

    JavaScript, Math · Oct 22, 2020

  • Median

    Calculates the median of an array of numbers.

    JavaScript, Math · Oct 22, 2020

  • Number to currency string

    Takes a number and returns it in the specified currency formatting.

    JavaScript, Math · Oct 22, 2020

  • Random hex color code

    Generates a random hexadecimal color code.

    JavaScript, Math · Jan 8, 2021

  • Number to ordinal suffix

    Takes a number and returns it as a string with the correct ordinal indicator suffix.

    JavaScript, Math · Nov 13, 2020

  • Hamming distance

    Calculates the Hamming distance between two values.

    JavaScript, Math · Dec 28, 2020

  • Map number to range

    Maps a number from one range to another range.

    JavaScript, Math · Oct 21, 2020

  • JavaScript's numeric separators explained

    Numeric separators are a somewhat lesser-known JavaScript syntactic sugar that can make working with large constants a lot easier.

    JavaScript, Math · Jun 27, 2021

  • Binomial coefficient

    Calculates the number of ways to choose k items from n items without repetition and without order.

    JavaScript, Math · Dec 28, 2020

  • Convert decimal number to hexadecimal

    Ever needed to convert a decimal number to hexadecimal? Here's a quick and easy way to do it.

    JavaScript, Math · Sep 21, 2022