Skip to content
the poor coder

how to get a random item from an array javascript

Poor Man
Published
Reading time
2 min read
On this page

How to get a random item from an array in Javascript?#

Getting a random item from an array in Javascript is a common problem that developers encounter. There are different methods to achieve this, but here are some of the most straightforward ones:

Method 1: Using Math.random() and Math.floor()#

One way to get a random item from an array is by using the Math.random() method and the Math.floor() method to generate a random index within the array. Here's how to do it:

Text

let arr = ['apple', 'banana', 'orange', 'kiwi'];
let randomItem = arr[Math.floor(Math.random() * arr.length)];
console.log(randomItem);
  • The "arr" variable holds the array of items you want to pick from.
  • The "Math.random()" method generates a random number between 0 and 1 (not including 1).
  • The "Math.floor()" method rounds down the random number generated by "Math.random()" to the nearest integer.
  • The "arr.length" property gets the length of the array.
  • The "randomItem" variable gets assigned the item at the index generated by "Math.floor(Math.random() * arr.length)".
  • Finally, we log the "randomItem" variable to the console to check if it worked.

Method 2: Using a for loop and Array.splice()#

Another way to get a random item from an array is by using a for loop and the Array.splice() method to remove a random item from the array. Here's how to do it:

Text

let arr = ['apple', 'banana', 'orange', 'kiwi'];
let randomItemIndex = Math.floor(Math.random() * arr.length);
let randomItem = arr[randomItemIndex];
arr.splice(randomItemIndex, 1);
console.log(randomItem);
console.log(arr);
  • The "arr" variable holds the array of items you want to pick from.
  • The "Math.random()" method generates a random number between 0 and 1 (not including 1).
  • The "Math.floor()" method rounds down the random number generated by "Math.random()" to the nearest integer.
  • The "arr.length" property gets the length of the array.
  • The "randomItemIndex" variable gets assigned the index of a random item in the array.
  • The "randomItem" variable gets assigned the item at the index generated by "randomItemIndex".
  • The "Array.splice()" method removes one element from the array starting at the index generated by "randomItemIndex".
  • Finally, we log the "randomItem" variable and the updated "arr" array to the console to check if it worked.

These are just two ways of getting a random item from an array in Javascript. There are many other methods and variations depending on your needs and preferences. The most important thing is to understand how arrays and random numbers work in Javascript and choose the method that works best for your particular case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC