It’s a simple one-liner:
const randomElement = array[Math.floor(Math.random() * array.length)];
For example:
const months = ["January", "February", "March", "April", "May", "June", "July"]; const random = Math.floor(Math.random() * months.length); console.log(random, months[random]);
Related Posts:
- Generate random number between two numbers in JavaScript
- Generating random whole numbers in JavaScript in a specific range?
- How to randomize (shuffle) a JavaScript array?
- Is there a simple way to make a random selection from an array in JavaScript or any other programming language?
- Seeding the random number generator in Javascript
- addEventListener vs onclick
- How do I check if an array includes a value in JavaScript?
- JavaScript document.getElementById().value logs empty value
- What is console.log?
- Can I use complex HTML with Twitter Bootstrap’s Tooltip?
- How do I redirect to another webpage?
- Copy array by value
- Compare two dates with JavaScript
- How can I merge properties of two JavaScript objects dynamically?
- In reactJS, how to copy text to clipboard?
- SyntaxError: Adjacent JSX elements must be wrapped in an enclosing tag
- Uncaught SyntaxError: Invalid or unexpected token
- JavaScript TypeError: Cannot read property ‘style’ of null
- Invalid shorthand property initializer
- nodejs – error self signed certificate in certificate chain
- Make javascript alert Yes/No Instead of Ok/Cancel
- How to create a dialog with “yes” and “no” options
- Adding images to a Bootstrap Carousel with AJAX
- XMLHttpRequest cannot load XXX No ‘Access-Control-Allow-Origin’ header
- Uncaught TypeError: Cannot read property ‘props’ of null
- How to use iMonkey in an iOS app
- ES6: Create Strings using Template Literals – Freecodecamp
- Youtube api – stop video
- Uncaught TypeError: Cannot read property ‘msie’ of undefined – jQuery tools
- Angular JS Uncaught Error: [$injector:modulerr]
- Nested objects in javascript, best practices
- How do I convert an integer to binary in JavaScript?
- What does window.jQuery and window.$ mean?
- How to change date format in JavaScript
- OnChange event handler for radio button (INPUT type=”radio”) doesn’t work as one value
- InvalidValueError: not an instance of HTMLInputElement
- How to clear react-native cache?
- Execute PHP function with onclick
- blocked a frame of origin “null” from accessing a cross-origin frame – chrome
- How can I resolve the error “URL scheme must be “http” or “https” for CORS request.” for this code [duplicate]
- How to set Angular 4 background image?
- React-Router : What is the purpose of IndexRoute?
- SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data
- Add new attribute (element) to JSON object using JavaScript
- D3 Appending Text to a SVG Rectangle
- How to implement “select all” check box in HTML?
- JQuery get the title of a button
- How to open a URL using PHP?
- Trying to load jquery into tampermonkey script
- Atom JavaScript Autocomplete
- How to format a UTC date as a `YYYY-MM-DD hh:mm:ss` string using NodeJS?
- JavaScript button onclick not working
- Invariant Violation: _registerComponent(…): Target container is not a DOM element
- How to count string occurrence in string?
- Angular 2 Hover event
- getElementsByClassName not working [duplicate]
- Javascript: Unicode string to hex
- Ajax success function
- What is the equivalent of Java’s System.out.println() in Javascript?
- How can I exit from a javascript function? [duplicate]
- Object doesn’t support property or method ‘addEventListener’
- How to fix javascript .toFixed is not a Function error
- jQuery checkbox checked state changed event
- Implementing Isomorphic JavaScript (React JS) in WordPress?
- Any advantage of using wp_scripts and is_IE when enqueuing scripts
- TinyMCE in a div / textarea on frontend?
- Include Javascript as Plain (No file inclusion)
- Adding wp_ajax to a theme outside functions.php (on specific template page)
- Insert shortcode in post editor from javascript (Visual / HTML)
- WordPress problem with htmlentities
- Provide specific example for block variation
- Block editor: How to check if block editor has initialized and populated the data store?
- VB.net and WordPress
- Gutenberg passing block attributes to component in ES6/ESNext
- How to add a custom.js file for a child-theme in a swift way?
- WP REST API Post Status Using JavaScript
- var is undefined in a Gutenberg block
- Custom wp.editor.initialize settings ignored
- Why my javascript code not working for the dashboard of wordpress admin?
- Detect When User Reads Full Article? [closed]
- Got a TypeError after update and need to adapt found solution
- How to add classes and events to image in javascript using Gutenberg?
- How do you filter the list of states when country is selected? [closed]
- Prevent update_checkout from firing mid-typing
- Adding Code into Theme Customizer Header
- How to move core js files into the footer
- Suddenly extra hex string in tags
- Including Styles and JS files to work ON my plugin interface
- Gutenberg – useEffect manipulate DOM after block re-renders DOM
- Why does my array sort order changes when I pass it to JS using WP_REST_Response?
- Javascript working on jsfiddle but not wordpress site?
- Drodown list return issue
- Spans in gutenberg
- Get a default customizeAction text for a section using Customizer JS API
- Put dynamic Javascript in header after doing operations
- custom page url slug needs illegal ?id=1 for javascript
- Use one javascript variable into another javascript file
- How to load JavaScript modules with script_loader_tag hook?
- How to securely set dynamic HTML content with JavaScript?
- Astra Mobile Cart Drawer is blank on home page