how to get a random element of an array javascript
snippet in javascript

javascript how to get a random element from an array

user7790

var items = ['Yes', 'No', 'Maybe'];
var item = items[Math.floor(Math.random() * items.length)];

how to get a random element of an array javascript

user1258

var foodItems = ["Bannana", "Apple", "Orange"];
var theFood = foodItems[Math.floor(Math.random() * foodItems.length)];
/* Will pick a random number from the length of the array and will go to the
corosponding number in the array E.G: 0 = Bannana */

pick a random element from an array javascript

user9413

var myArray = [
  "Apples",
  "Bananas",
  "Pears"
];

var randomItem = myArray[Math.floor(Math.random()*myArray.length)];

javascript get first element of array

user3216

let array = ["a", "b", "c", "d", "e"];

// Both first1 and first2 have the same value.
let [first1] = array;
let first2 = array[0];