Consider:
var myArray = ['January', 'February', 'March'];
How can I select a random value from this array using JavaScript?
Consider:
var myArray = ['January', 'February', 'March'];
How can I select a random value from this array using JavaScript?
This work like a charm for me without any repeat.
~~
is much faster thanMath.Floor()
, so when it comes to performance optimization while producing output using UI elements,~~
wins the game. MORE INFOBut if you know that the array is going to have millions of elements than you might want to reconsider between Bitwise Operator and
Math.Floor()
, as bitwise operator behave weirdly with large numbers. See below example explained with the output. MORE INFOI've found a way around the top answer's complications, just by concatenating the variable rand to another variable that allows that number to be displayed inside the calling of myArray[];. By deleting the new array created and toying around with it's complications, I've come up with a working solution:
Using Faker.js:
If you have fixed values (like a month name list) and want a one-line solution
The second part of the array is an access operation as described in Why does [5,6,8,7][1,2] = 8 in JavaScript?