<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2" -->

---
title: Display a Random Image with JavaScript or the Unsplash API
description: You can show a random image with a few lines of JavaScript: keep an array of image paths, pick one with Math.floor(Math.random() * images.length), and set an...
canonical: https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Display a Random Image with JavaScript or the Unsplash API | daily.dev
og:description: You can show a random image with a few lines of JavaScript: keep an array of image paths, pick one with Math.floor(Math.random() * images.length), and set an...
og:url: https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2
og:image: https://api.daily.dev/og/posts/whvNNXDa2.png
og:image:alt: Display a Random Image with JavaScript or the Unsplash API
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Display a Random Image with JavaScript or the Unsplash API

**[Kirupa](https://daily.dev/sources/kirupa)** · 15 min read · 0 upvotes · 0 comments

## Summary

You can show a random image with a few lines of JavaScript: keep an array of image paths, pick one with Math.floor(Math.random() * images.length), and set an img element's src to it. A button that calls the same function swaps images without reloading, and picking again until the result differs avoids showing the same one twice. The old source.unsplash.com shortcut was shut down in 2024. Lorem Picsum needs no key. For the Unsplash API, a small Node 18 server keeps your access key out of the browser. New apps start in demo mode, which allows 50 requests an hour.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://www.kirupa.com/html5/displaying_a_random_image.htm>

## Questions this post answers

### Why does source.unsplash.com/random show a broken image now?

Unsplash deprecated the source.unsplash.com shortcut in 2021 and shut it down in 2024, so it now just returns an error. Code that relies on it displays broken image icons. Replace it with the official Unsplash API, which needs an access key, or with a no-key service like Lorem Picsum.

_Developers keep up with deprecated web APIs like this on daily.dev._

### How do I stop my random image from repeating when I click a button in JavaScript?

Keep picking a random image until it differs from the one currently shown. Compare the candidate against the img element's src attribute, and check that the array holds more than one image so the loop cannot run forever. Move the images array outside the picker function so both functions can read it.

_Developers sharpening everyday JavaScript patterns like this follow them on daily.dev._

### How can I use the Unsplash API for random photos without exposing my access key in the browser?

Call Unsplash from your own server so the key never reaches client-side JavaScript. The page requests a photo from your endpoint, and the server adds the Client-ID Authorization header, calls /photos/random, and returns trimmed JSON. Read the key from an environment variable. Demo-mode apps are limited to 50 requests per hour.

_Developers handling API keys and server-side proxies follow that kind of practical security guidance on daily.dev._

## Similar posts on daily.dev

- [A Button That Links to a Random Post in HTML & CSS Only – Master.dev Blog](https://daily.dev/posts/a-button-that-links-to-a-random-post-in-html-css-only-master-dev-blog-gzrkkvvhg) · Frontend Masters · 3 upvotes · 1 comments
- [How to Load a Random HTML Page Inline with JavaScript](https://daily.dev/posts/how-to-load-a-random-html-page-inline-with-javascript-lg49qvbvs) · Kirupa · 0 upvotes · 0 comments
- [OpenAI Image Generation](https://daily.dev/posts/openai-image-generation-kls9o1fnw) · Telerik · 1 upvotes · 0 comments
- [Embracing Playful Imperfection With The CSS random\(\) Function — Smashing Magazine](https://daily.dev/posts/embracing-playful-imperfection-with-the-css-random-function-smashing-magazine-cwvipesbh) · Smashing Magazine · 0 upvotes · 1 comments

---

Tags: [#javascript](https://daily.dev/tags/javascript), [#nodejs](https://daily.dev/tags/nodejs), [#html](https://daily.dev/tags/html)

[View this post on daily.dev](https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Display a Random Image with JavaScript or the Unsplash API","url":"https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2"},"datePublished":"2026-11-28T06:30:37.656Z","dateModified":"2026-11-28T06:31:04.263Z","description":"You can show a random image with a few lines of JavaScript: keep an array of image paths, pick one with Math.floor(Math.random() * images.length), and set an...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/70d14f1e97fc82fab792e305f35ba11e?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/70d14f1e97fc82fab792e305f35ba11e?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Kirupa","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Kirupa","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/ff2fdb4927d34b81bd07cd18d6049a98","url":"https://daily.dev/sources/kirupa"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"javascript,nodejs,html","timeRequired":"PT15M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Kirupa","item":"https://daily.dev/sources/kirupa"},{"@type":"ListItem","position":3,"name":"Display a Random Image with JavaScript or the Unsplash API"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/display-a-random-image-with-javascript-or-the-unsplash-api-whvnnxda2#faq","mainEntity":[{"@type":"Question","name":"Why does source.unsplash.com/random show a broken image now?","acceptedAnswer":{"@type":"Answer","text":"Unsplash deprecated the source.unsplash.com shortcut in 2021 and shut it down in 2024, so it now just returns an error. Code that relies on it displays broken image icons. Replace it with the official Unsplash API, which needs an access key, or with a no-key service like Lorem Picsum. Developers keep up with deprecated web APIs like this on daily.dev."}},{"@type":"Question","name":"How do I stop my random image from repeating when I click a button in JavaScript?","acceptedAnswer":{"@type":"Answer","text":"Keep picking a random image until it differs from the one currently shown. Compare the candidate against the img element's src attribute, and check that the array holds more than one image so the loop cannot run forever. Move the images array outside the picker function so both functions can read it. Developers sharpening everyday JavaScript patterns like this follow them on daily.dev."}},{"@type":"Question","name":"How can I use the Unsplash API for random photos without exposing my access key in the browser?","acceptedAnswer":{"@type":"Answer","text":"Call Unsplash from your own server so the key never reaches client-side JavaScript. The page requests a photo from your endpoint, and the server adds the Client-ID Authorization header, calls /photos/random, and returns trimmed JSON. Read the key from an environment variable. Demo-mode apps are limited to 50 requests per hour. Developers handling API keys and server-side proxies follow that kind of practical security guidance on daily.dev."}}]}
```

