The arrSample() method returns a random element from the array. Returns undefined if the array is empty or null/undefined.
Identifies the base type of array elements
The array or array-like object to sample from
A random element from the array, or undefined if empty
0.14.0
arrSample([1, 2, 3, 4, 5]); // e.g., 3arrSample(["a", "b", "c"]); // e.g., "b"arrSample([42]); // 42arrSample([]); // undefinedarrSample(null); // undefined// Array-like objectsarrSample({ length: 3, 0: "x", 1: "y", 2: "z" }); // e.g., "y" Copy
arrSample([1, 2, 3, 4, 5]); // e.g., 3arrSample(["a", "b", "c"]); // e.g., "b"arrSample([42]); // 42arrSample([]); // undefinedarrSample(null); // undefined// Array-like objectsarrSample({ length: 3, 0: "x", 1: "y", 2: "z" }); // e.g., "y"
The arrSample() method returns a random element from the array. Returns undefined if the array is empty or null/undefined.