CyrefJS
CCyrefJS
فا
Back to documentation
Array/Since 1.38.0

first

Returns the first element of an array.

first(array):T | undefined

Parameters

arrayT[]required

The array from which to retrieve the first element.

Returns

T | undefined

The first element of the array, or undefined if the array is empty.

Examples

01

Basic usage

Get the first element from an array.

TypeScript
import { first } from "@cyref/js";

const numbers = [10, 20, 30, 40];

const result = first(numbers);

console.log(result);
// 10
02

With strings

Get the first string from an array.

TypeScript
import { first } from "@cyref/js";

const languages = ["JavaScript", "TypeScript", "Python"];

const result = first(languages);

console.log(result);
// "JavaScript"
03

Empty array

If the array is empty, the function returns undefined.

TypeScript
import { first } from "@cyref/js";

const values: number[] = [];

const result = first(values);

console.log(result);
// undefined
Built with@cyref/js·Official documentation