Get Random Array Item
Get a random item from a given array.
| Category | Random |
| Author | Pol Gubau |
| Since | 1.0.4 |
| Last Updated | 2025-03-17 |
| Type | Sync |
| Compatibility | Universal |
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
| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| array | T[] | null | No | The 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