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.

### 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

### Mapped array sum

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

JavaScript, Math · Nov 3, 2020

### Geometric progression

Initializes an array containing the numbers in the specified geometric progression range.

JavaScript, Math · Dec 28, 2020

### Integer to roman numeral

Converts an integer to its roman numeral representation. Accepts value between

`1`

and`3999`

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

### N max elements

Returns the

`n`

maximum elements from the provided array.JavaScript, Math · Oct 21, 2020

### N min elements

Returns the

`n`

minimum elements from the provided array.JavaScript, Math · Oct 21, 2020

### Pretty-print number of bytes

Converts a number in bytes to a human-readable string.

JavaScript, String · Oct 22, 2020

### 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, Math · May 8, 2022

### Cross product of arrays

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

JavaScript, Math · Oct 22, 2020

### Max array value based on function

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

JavaScript, Math · Oct 21, 2020

### Min array value based on function

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

JavaScript, Math · Oct 21, 2020

### Insertion index in sorted array based on function

Finds the lowest insertion index to maintain array sorting order based on the provided iterator function.

JavaScript, Array · Oct 22, 2020

### Fibonacci

Generates an array, containing the Fibonacci sequence, up until the nth term.

JavaScript, Math · Dec 28, 2020