For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /reference/array/initial.md.

initial es-toolkit

Returns a new array containing all elements except the last one.

const result = initial(arr);

Usage

initial(arr)

Use initial when you want to get all elements except the last one from an array. If the array is empty or has only one element, it returns an empty array. This is useful when processing while excluding the end of the array.

import { initial } from 'es-toolkit/array';

// Exclude the last element from a number array
const numbers = [1, 2, 3, 4, 5];
initial(numbers);
// Returns: [1, 2, 3, 4]

// Exclude the last element from a string array
const strings = ['a', 'b', 'c'];
initial(strings);
// Returns: ['a', 'b']

// An array with only one element returns an empty array
const single = [42];
initial(single);
// Returns: []

It safely handles empty arrays and special cases.

import { initial } from 'es-toolkit/array';

// An empty array returns an empty array
const empty: number[] = [];
initial(empty);
// Returns: []

// It can also handle nested arrays
const nested = [
  [1, 2],
  [3, 4],
  [5, 6],
];
initial(nested);
// Returns: [[1, 2], [3, 4]]

Parameters

arrreadonly T[]required

The array from which to exclude the last element.

Returns

Returns a new array excluding the last element. If the input array is empty or has only one element, it returns an empty array.

T[]
Source: es-toolkit

Re-exported verbatim from es-toolkit. Implementation, edge cases, and performance behavior are owned upstream. This page mirrors the documentation at the pinned version; the linked source is authoritative.