Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

Build a Random Quote Generator App with JavaScript

A random quote generator is a small app with a real use: people open it for a quick dose of motivation. It is also a perfect beginner project — an array of data, one button, and a little DOM work. You can finish the core

A random quote generator is a small app with a real use: people open it for a quick dose of motivation. It is also a perfect beginner project — an array of data, one button, and a little DOM work. You can finish the core in under an hour and then keep polishing it.

What you are building

A card that shows one quote and its author, a "New Quote" button that picks another one at random, and a copy button so the user can share the quote. A background colour that changes with each quote is a nice touch.

The data

Start with 10 to 15 quotes you actually like. Store them as objects so the quote and the author stay together:

const quotes = [
  { text: "The best time to plant a tree was 20 years ago. The second best time is now.", author: "Chinese Proverb" },
  { text: "It does not matter how slowly you go as long as you do not stop.", author: "Confucius" },
  { text: "Everything you have ever wanted is on the other side of fear.", author: "George Addair" },
  { text: "Success is not final, failure is not fatal: it is the courage to continue that counts.", author: "Winston Churchill" },
  { text: "Believe you can and you are halfway there.", author: "Theodore Roosevelt" },
];

Picking a random quote

Avoid showing the same quote twice in a row — it is a small detail users notice:

let lastIndex = -1;

function newQuote() {
  let index;
  do {
    index = Math.floor(Math.random() * quotes.length);
  } while (index === lastIndex && quotes.length > 1);

  lastIndex = index;
  document.getElementById("quote").textContent = quotes[index].text;
  document.getElementById("author").textContent = "— " + quotes[index].author;
}

document.getElementById("new-quote").addEventListener("click", newQuote);
newQuote();

Copy to clipboard

One line with the Clipboard API makes the app genuinely useful:

document.getElementById("copy").addEventListener("click", async () => {
  const text = document.getElementById("quote").textContent + " " + document.getElementById("author").textContent;
  await navigator.clipboard.writeText(text);
  document.getElementById("copy").textContent = "Copied!";
  setTimeout(() => (document.getElementById("copy").textContent = "Copy"), 1500);
});

Why quotes work

A quote app works because it matches a real habit: people look for one good line to start the day. If you want to go deeper than single lines, reading full books is the next step — for example, this list of 15 best self-help books in Hindi collects the books that those famous one-liners usually come from.

What to add next

Add categories (motivation, focus, patience), a "quote of the day" that changes by date instead of randomly, or fetch quotes from a free public API so the list never runs out. Saving favourites in localStorage is another good stretch.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.