Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

std/collections/mod.ts>sample

Deno standard library
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
function sample
import { sample } from "https://deno.land/std@0.194.0/collections/mod.ts";

Returns a random element from the given array

Examples

Example 1

import { sample } from "https://deno.land/std@0.194.0/collections/sample.ts";
import { assert } from "https://deno.land/std@0.194.0/testing/asserts.ts";

const numbers = [1, 2, 3, 4];
const random = sample(numbers);

assert(numbers.includes(random as number));

Parameters

array: readonly T[]

Returns

T | undefined