Get Random Array Item

Get a random item from a given array.

CategoryRandom
AuthorPol Gubau
Since1.0.4
Last Updated2025-03-17
TypeSync
CompatibilityUniversal

Example

  • Input

    getRandomArrayItem([1, 2, 3, 4, 5])

    Output:

    3 // (random value, could be any from the array)
  • Input

    getRandomArrayItem(['apple', 'banana', 'cherry'])

    Output:

    "banana" // (random value, could be any from the array)

Return

A random item from the array, or undefined if the array is empty.

Type: T | undefined

Props

NameTypeDefaultRequiredDescription
arrayT[]nullNoThe input array to pick a random item from.

Notes

This function returns `undefined` if the input array is empty or not a valid array.

Tags

  • random
  • array
  • utilities

Related