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

### Remove list elements from the end

JavaScript, Array

Creates an array with

`n`

elements removed from the end.### Array unique symmetric difference

JavaScript, Array

Returns the unique symmetric difference between two arrays, not containing duplicate values from either array.

### Check if all array elements are unique

JavaScript, Array

Checks if all elements in an array are unique.

### Average of numbers

JavaScript, Math

Calculates the average of two or more numbers.

### Compact array

JavaScript, Array

Removes falsy values from an array.

### Product of numeric values

JavaScript, Math

Calculates the product of two or more numbers/arrays.

### Array similarity

JavaScript, Array

Returns an array of elements that appear in both arrays.

### Array sum

JavaScript, Math

Calculates the sum of two or more numbers/arrays.

### Count occurrences

JavaScript, Array

Counts the occurrences of a value in an array.

### Every nth element

JavaScript, Array

Returns every

`nth`

element in an array.### Initialize 2D array

JavaScript, Array

Initializes a 2D array of given width and height and value.

### Array intersection

JavaScript, Array

Returns the elements that exist in both arrays, filtering duplicate values.

### Nth element

JavaScript, Array

Returns the nth element of an array.

### Read file lines

JavaScript, Node

Returns an array of lines from the specified file.

### Insertion index in sorted array

JavaScript, Array

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