# How to get a random value from an array with JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Apr 3, 2020, updated Oct 15, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-get-random-value-from-array

> A quick guide to learn how to select a random value from an array in JavaScript.

To select a random value from an [array](https://attacomsian.com/blog/javascript-arrays) in JavaScript, use the `Math` object functions. 

Let us say we have the following fruits array:

```javascript
const fruits = [
    "Apple",
    "Orange",
    "Mango",
    "Banana",
    "Cherry"
]
```

Now, we want to create a function that selects a random fruit from an array of fruits.

Let us write a `randomValue()`  function that randomly picks a value from the given list:

```javascript
const randomValue = (list) => {
    return list[Math.floor(Math.random() * list.length)]
}
```

Now you can use the above function to select a random value from an array, like below:

```javascript
console.log(randomValue(fruits)) // Banana
```
