Featured Article

Random Image JavaScript Tutorial W3Schools Guide

Adam Jul 13, 2026

Ever needed to inject a bit of randomness into your web pages? One way to achieve this is by displaying random images using JavaScript. The W3Schools, a renowned online learning resource for web developers, offers a simple yet efficient method to accomplish this. Let's explore how to use W3Schools' guidance to create a captivating, dynamic web experience with randomly displayed images.

Javascript Pixel Art
Javascript Pixel Art

Before we dive into the JavaScript part, let's ensure we have a collection of images ready. You might have an array of image paths stored in your script or fetch them dynamically from an API. For this example, let's assume we have an array of image paths stored in a variable named `imageArray`.

Lain iwakura
Lain iwakura

Fetching Random Images

The first step is to retrieve a random image from our array. We can achieve this by using the `Math.floor()` and `Math.random()` functions together with `imageArray.length` to generate a random index within the array's scope.

CSS W3 Course
CSS W3 Course

Here's a simple function that fetches a random image using the `imageArray`: ```javascript function getRandomImage() { return imageArray[Math.floor(Math.random() * imageArray.length)]; } ``` This function will return a random image path from the `imageArray` each time it's called.

Array of Image Paths

Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript

Let's say you have an array of image paths like this: ```javascript const imageArray = [ 'image1.jpg', 'image2.png', 'image3.jpeg', // Add more image paths as needed ]; ``` You can store this array in your JavaScript file or dynamically populate it based on your requirements.

Now, let's see how to display this random image on our webpage.

Displaying the Random Image

JavaScript Basics You MUST Know
JavaScript Basics You MUST Know

To display the random image, you can create an `img` element using JavaScript, set its `src` attribute to the path returned by the `getRandomImage()` function, and then append it to your webpage. Here's a simple way to do it: ```javascript let img = document.createElement('img'); img.src = getRandomImage(); document.body.appendChild(img); ``` This code will create an `img` element, set its source to a random image from the `imageArray`, and append it to the `body` of your webpage.

But what if you want to update the image every time a button is clicked or after a certain interval? Let's explore these functionalities in the next section.

Updating the Image Dynamically

an image of windows on the screen with different icons and symbols coming out of it
an image of windows on the screen with different icons and symbols coming out of it

To update the image after a certain interval, you can use the `setInterval()` function. This function takes two arguments: a callback to run (in this case, the `getRandomImage()` function and the image display code) and the interval at which to run it (in milliseconds).

Here's an example: ```javascript let intervalId = setInterval(function() { let img = document.createElement('img'); img.src = getRandomImage(); document.body.appendChild(img); // Remove the previous image to avoid piling them up document.body.firstChild.remove(); }, 5000); // Change the interval value (in ms) as desired ``` This code will create a new random image every 5 seconds and append it to the `body`, while also removing the previous image. Remember to pause or clear the interval when it's no longer needed to avoid memory leaks.

an image of a computer screen with many lines and numbers on the screen, all in different colors
an image of a computer screen with many lines and numbers on the screen, all in different colors
javascript
javascript
Guy with laptop
Guy with laptop
a computer screen with an image of a penguin on it and the words i'm glob attached to linux
a computer screen with an image of a penguin on it and the words i'm glob attached to linux
a cartoon penguin with sunglasses on its head
a cartoon penguin with sunglasses on its head
...
...
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
Java vs javascript
Java vs javascript
the collage has many different types of logos on it, including an apple computer
the collage has many different types of logos on it, including an apple computer
Download Canvas As Image In Javascript
Download Canvas As Image In Javascript
Create a RAIN EFFECT with HTML CSS and JavaScript NOW!
Create a RAIN EFFECT with HTML CSS and JavaScript NOW!
sunset evening using html css javascript
sunset evening using html css javascript
an old computer game screen with some type of information on the bottom right hand corner
an old computer game screen with some type of information on the bottom right hand corner
the cover of an electronic book with words coming out of it, in front of a black background
the cover of an electronic book with words coming out of it, in front of a black background
an image of some type of computer screen with space in the background and stars above it
an image of some type of computer screen with space in the background and stars above it
Javascript Window.print()
Javascript Window.print()
Resize Image In Javascript
Resize Image In Javascript
Image Hover Animation in HTML & CSS
Image Hover Animation in HTML & CSS
HTML
HTML
Coding Inspiration For Programmers, Vscode Themes For Programmers, Web Programming Aesthetic, Eprom Programmer Software Interface, Computer Languages, Computer Coding Aesthetic, How To Become A System Programmer, Computer Programmer Aesthetic, Competitive Coding / Programming
Coding Inspiration For Programmers, Vscode Themes For Programmers, Web Programming Aesthetic, Eprom Programmer Software Interface, Computer Languages, Computer Coding Aesthetic, How To Become A System Programmer, Computer Programmer Aesthetic, Competitive Coding / Programming

If you want to update the image when a button is clicked instead, you can add an event listener to your button. Here's how you can do it: ```javascript document.getElementById('myButton').addEventListener('click', function() { let img = document.createElement('img'); img.src = getRandomImage(); document.body.appendChild(img); // Remove the previous image as done before document.body.firstChild.remove(); }); ``` Don't forget to replace `'myButton'` with the actual ID of your button.

Now that you've learned how to show random images using JavaScript and the guidance from W3Schools, you can create engaging and dynamic web experiences for your users. Happy coding!