Skip to main content
Porffor implements the full Array API for array manipulation and iteration.

Array Constructor

Array

Creates a new array.
Array(...items: any[]): any[]
Array(length: number): any[]
const arr1 = Array(1, 2, 3);     // [1, 2, 3]
const arr2 = Array(5);           // Array of length 5 with undefined elements
const arr3 = Array();            // []

Static Methods

Array.isArray

Checks if a value is an array.
Array.isArray(value: any): boolean
console.log(Array.isArray([1, 2, 3]));  // true
console.log(Array.isArray('hello'));    // false
console.log(Array.isArray({ }));        // false

Array.from

Creates an array from an iterable or array-like object.
Array.from(arrayLike: any, mapFn?: Function, thisArg?: any): any[]
const str = 'hello';
console.log(Array.from(str));  // ['h', 'e', 'l', 'l', 'o']

const arr = Array.from([1, 2, 3], x => x * 2);
console.log(arr);  // [2, 4, 6]

const set = new Set([1, 2, 3]);
console.log(Array.from(set));  // [1, 2, 3]

Instance Methods

at

Returns the element at an index (supports negative indexing).
array.at(index: number): any
const arr = [1, 2, 3, 4, 5];
console.log(arr.at(0));   // 1
console.log(arr.at(-1));  // 5
console.log(arr.at(-2));  // 4

concat

Concatenates arrays.
array.concat(...items: any[]): any[]
const arr1 = [1, 2];
const arr2 = [3, 4];
const arr3 = arr1.concat(arr2, [5, 6]);
console.log(arr3);  // [1, 2, 3, 4, 5, 6]

push

Adds elements to the end of an array.
array.push(...items: any[]): number
const arr = [1, 2];
const newLength = arr.push(3, 4);
console.log(arr);        // [1, 2, 3, 4]
console.log(newLength);  // 4

pop

Removes and returns the last element.
array.pop(): any
const arr = [1, 2, 3];
const last = arr.pop();
console.log(last);  // 3
console.log(arr);   // [1, 2]

shift

Removes and returns the first element.
array.shift(): any
const arr = [1, 2, 3];
const first = arr.shift();
console.log(first);  // 1
console.log(arr);    // [2, 3]

unshift

Adds elements to the beginning of an array.
array.unshift(...items: any[]): number
const arr = [3, 4];
const newLength = arr.unshift(1, 2);
console.log(arr);        // [1, 2, 3, 4]
console.log(newLength);  // 4

slice

Extracts a section of an array.
array.slice(start?: number, end?: number): any[]
const arr = [1, 2, 3, 4, 5];
console.log(arr.slice(1, 4));  // [2, 3, 4]
console.log(arr.slice(2));     // [3, 4, 5]
console.log(arr.slice(-2));    // [4, 5]

splice

Changes array contents by removing/replacing/adding elements.
array.splice(start: number, deleteCount?: number, ...items: any[]): any[]
const arr = [1, 2, 3, 4, 5];

// Remove 2 elements starting at index 1
const removed = arr.splice(1, 2);
console.log(removed);  // [2, 3]
console.log(arr);      // [1, 4, 5]

// Insert elements
arr.splice(1, 0, 2, 3);
console.log(arr);      // [1, 2, 3, 4, 5]

// Replace elements
arr.splice(2, 1, 10);
console.log(arr);      // [1, 2, 10, 4, 5]

indexOf

Returns the first index of an element.
array.indexOf(searchElement: any, fromIndex?: number): number
const arr = [1, 2, 3, 2, 1];
console.log(arr.indexOf(2));     // 1
console.log(arr.indexOf(2, 2));  // 3
console.log(arr.indexOf(5));     // -1

lastIndexOf

Returns the last index of an element.
array.lastIndexOf(searchElement: any, fromIndex?: number): number
const arr = [1, 2, 3, 2, 1];
console.log(arr.lastIndexOf(2));  // 3
console.log(arr.lastIndexOf(1));  // 4

includes

Checks if array contains an element.
array.includes(searchElement: any, fromIndex?: number): boolean
const arr = [1, 2, 3];
console.log(arr.includes(2));     // true
console.log(arr.includes(5));     // false
console.log(arr.includes(1, 1));  // false

reverse

Reverses the array in place.
array.reverse(): any[]
const arr = [1, 2, 3, 4, 5];
arr.reverse();
console.log(arr);  // [5, 4, 3, 2, 1]

sort

Sorts the array in place.
array.sort(compareFn?: (a, b) => number): any[]
// Default sort (lexicographic)
const arr1 = [3, 1, 4, 1, 5, 9];
arr1.sort();
console.log(arr1);  // [1, 1, 3, 4, 5, 9]

// Numeric sort
const arr2 = [3, 1, 4, 1, 5, 9];
arr2.sort((a, b) => a - b);
console.log(arr2);  // [1, 1, 3, 4, 5, 9]

// Reverse numeric sort
const arr3 = [3, 1, 4, 1, 5, 9];
arr3.sort((a, b) => b - a);
console.log(arr3);  // [9, 5, 4, 3, 1, 1]

join

Joins array elements into a string.
array.join(separator?: string): string
const arr = ['Hello', 'World'];
console.log(arr.join(' '));   // 'Hello World'
console.log(arr.join(', '));  // 'Hello, World'
console.log(arr.join(''));    // 'HelloWorld'

Iteration Methods

forEach

Executes a function for each element.
array.forEach(callback: (element, index, array) => void, thisArg?: any): void
const arr = [1, 2, 3];
arr.forEach((x, i) => {
  console.log(`arr[${i}] = ${x}`);
});
// arr[0] = 1
// arr[1] = 2
// arr[2] = 3

map

Creates a new array with results of calling a function on every element.
array.map(callback: (element, index, array) => any, thisArg?: any): any[]
const arr = [1, 2, 3];
const doubled = arr.map(x => x * 2);
console.log(doubled);  // [2, 4, 6]

const indexed = arr.map((x, i) => `${i}: ${x}`);
console.log(indexed);  // ['0: 1', '1: 2', '2: 3']

filter

Creates a new array with elements that pass a test.
array.filter(callback: (element, index, array) => boolean, thisArg?: any): any[]
const arr = [1, 2, 3, 4, 5];
const evens = arr.filter(x => x % 2 === 0);
console.log(evens);  // [2, 4]

const greaterThan2 = arr.filter(x => x > 2);
console.log(greaterThan2);  // [3, 4, 5]

reduce

Reduces array to a single value.
array.reduce(callback: (accumulator, element, index, array) => any, initialValue?: any): any
const arr = [1, 2, 3, 4, 5];

// Sum
const sum = arr.reduce((acc, x) => acc + x, 0);
console.log(sum);  // 15

// Product
const product = arr.reduce((acc, x) => acc * x, 1);
console.log(product);  // 120

// Max
const max = arr.reduce((acc, x) => Math.max(acc, x));
console.log(max);  // 5

reduceRight

Reduces array from right to left.
array.reduceRight(callback: (accumulator, element, index, array) => any, initialValue?: any): any
const arr = ['a', 'b', 'c'];
const reversed = arr.reduceRight((acc, x) => acc + x, '');
console.log(reversed);  // 'cba'

find

Returns the first element that satisfies a test.
array.find(callback: (element, index, array) => boolean, thisArg?: any): any
const arr = [1, 2, 3, 4, 5];
const found = arr.find(x => x > 3);
console.log(found);  // 4

findIndex

Returns the index of the first element that satisfies a test.
array.findIndex(callback: (element, index, array) => boolean, thisArg?: any): number
const arr = [1, 2, 3, 4, 5];
const index = arr.findIndex(x => x > 3);
console.log(index);  // 3

findLast

Returns the last element that satisfies a test.
array.findLast(callback: (element, index, array) => boolean, thisArg?: any): any
const arr = [1, 2, 3, 4, 5];
const found = arr.findLast(x => x > 2);
console.log(found);  // 5

findLastIndex

Returns the index of the last element that satisfies a test.
array.findLastIndex(callback: (element, index, array) => boolean, thisArg?: any): number
const arr = [1, 2, 3, 4, 5];
const index = arr.findLastIndex(x => x > 2);
console.log(index);  // 4

every

Tests if all elements pass a test.
array.every(callback: (element, index, array) => boolean, thisArg?: any): boolean
const arr = [2, 4, 6, 8];
console.log(arr.every(x => x % 2 === 0));  // true
console.log(arr.every(x => x > 5));        // false

some

Tests if at least one element passes a test.
array.some(callback: (element, index, array) => boolean, thisArg?: any): boolean
const arr = [1, 2, 3, 4, 5];
console.log(arr.some(x => x > 4));   // true
console.log(arr.some(x => x > 10));  // false

flat

Flattens nested arrays.
array.flat(depth?: number): any[]
const arr = [1, [2, 3], [4, [5, 6]]];
console.log(arr.flat());     // [1, 2, 3, 4, [5, 6]]
console.log(arr.flat(2));    // [1, 2, 3, 4, 5, 6]

flatMap

Maps and flattens the result.
array.flatMap(callback: (element, index, array) => any, thisArg?: any): any[]
const arr = [1, 2, 3];
const result = arr.flatMap(x => [x, x * 2]);
console.log(result);  // [1, 2, 2, 4, 3, 6]

fill

Fills array elements with a static value.
array.fill(value: any, start?: number, end?: number): any[]
const arr = [1, 2, 3, 4, 5];
arr.fill(0);
console.log(arr);  // [0, 0, 0, 0, 0]

const arr2 = [1, 2, 3, 4, 5];
arr2.fill(9, 2, 4);
console.log(arr2);  // [1, 2, 9, 9, 5]

copyWithin

Copies array elements within the array.
array.copyWithin(target: number, start?: number, end?: number): any[]
const arr = [1, 2, 3, 4, 5];
arr.copyWithin(0, 3);
console.log(arr);  // [4, 5, 3, 4, 5]

Usage Examples

Data Processing

const data = [
  { name: 'Alice', age: 30 },
  { name: 'Bob', age: 25 },
  { name: 'Charlie', age: 35 }
];

// Get names
const names = data.map(person => person.name);
console.log(names);  // ['Alice', 'Bob', 'Charlie']

// Filter by age
const adults = data.filter(person => person.age >= 30);
console.log(adults);  // [{ name: 'Alice', age: 30 }, { name: 'Charlie', age: 35 }]

// Average age
const avgAge = data.reduce((sum, person) => sum + person.age, 0) / data.length;
console.log(avgAge);  // 30

Array Manipulation

function removeDuplicates(arr) {
  return arr.filter((item, index) => arr.indexOf(item) === index);
}

const arr = [1, 2, 3, 2, 4, 3, 5];
console.log(removeDuplicates(arr));  // [1, 2, 3, 4, 5]

See Also

Build docs developers (and LLMs) love