Skip to main content

Function: first()

first<ElementType>(iterable: IterableResolvable<ElementType>): ElementType | undefined

Consumes the first element of the iterable, returning it if it's found and undefined otherwise.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>The iterator to return the first value of.

Returns

ElementType | undefined

The first value of the iterator, or undefined if the iterator is empty.

Example

import { first } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 3, 4, 5];
console.log(first(iterable));
// Output: 1

Remarks

This function consumes the first value of the iterator.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/first.ts:22