Picks a random element from an array.
sample([1, 2, 3]); // 2 (type: number | undefined)sample(["a", "b", "c"]); // "c" (type: string | undefined)sample([]); // undefined (type: undefined)const a = [1, 2, 3] as const;sample(a); // 2 (type: 1 | 2 | 3)sample([1, 1, 1] as [number, number, number]); // 1 (type: number) Copy
sample([1, 2, 3]); // 2 (type: number | undefined)sample(["a", "b", "c"]); // "c" (type: string | undefined)sample([]); // undefined (type: undefined)const a = [1, 2, 3] as const;sample(a); // 2 (type: 1 | 2 | 3)sample([1, 1, 1] as [number, number, number]); // 1 (type: number)
source array
Picks a random element from an array.
Example