X Xerobit

Generate Lorem Ipsum in JavaScript — Libraries and Custom Generators

Generate Lorem Ipsum placeholder text in JavaScript using faker.js, lorem-ipsum npm package, or a custom word-bank generator. Includes React hooks, Node.js scripts, and...

Mian Ali Khalid · · 5 min read
Use the tool
Lorem Ipsum Generator
Generate placeholder text — words, sentences, or paragraphs. Classic lorem ipsum plus alternatives (hipster, cupcake, pirate). HTML-wrapped output option.
Open Lorem Ipsum Generator →

Generating Lorem Ipsum in JavaScript is straightforward with the lorem-ipsum package or faker.js. Here’s how to add programmatic placeholder text generation to your project.

Use the Lorem Ipsum Generator for one-off text generation without code.

Quick option: lorem-ipsum package

npm install lorem-ipsum
import { LoremIpsum } from 'lorem-ipsum';

const lorem = new LoremIpsum({
  sentencesPerParagraph: { max: 8, min: 4 },
  wordsPerSentence: { max: 16, min: 4 },
});

// Generate text
const word = lorem.generateWords(1);
const sentence = lorem.generateSentences(1);
const paragraph = lorem.generateParagraphs(1);
const paragraphs = lorem.generateParagraphs(5);

Faker.js (realistic fake content)

npm install @faker-js/faker
import { faker } from '@faker-js/faker';

// Paragraph-style lorem ipsum:
const paragraph = faker.lorem.paragraph();      // 3 sentences
const paragraph5 = faker.lorem.paragraphs(3);  // 3 paragraphs

// Controlled length:
const sentence = faker.lorem.sentence(8);      // 8-word sentence
const words = faker.lorem.words(10);           // 10 words
const lines = faker.lorem.lines(4);            // 4 newline-separated lines

// For realistic content (not lorem ipsum):
const title = faker.lorem.sentence({ min: 3, max: 7 }); // title-length sentence

Custom lorem ipsum generator (no dependencies)

const LOREM_WORDS = [
  'lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur',
  'adipiscing', 'elit', 'sed', 'do', 'eiusmod', 'tempor',
  'incididunt', 'ut', 'labore', 'dolore', 'magna', 'aliqua',
  'enim', 'minim', 'veniam', 'quis', 'nostrud', 'exercitation',
  'ullamco', 'laboris', 'nisi', 'aliquip', 'commodo', 'consequat',
  'duis', 'aute', 'irure', 'reprehenderit', 'voluptate', 'velit',
  'esse', 'cillum', 'fugiat', 'nulla', 'pariatur',
];

function randomItem(arr) {
  return arr[Math.floor(Math.random() * arr.length)];
}

function capitalize(str) {
  return str.charAt(0).toUpperCase() + str.slice(1);
}

function generateSentence(wordCount = null) {
  const count = wordCount ?? Math.floor(Math.random() * 12) + 6;
  const words = Array.from({ length: count }, () => randomItem(LOREM_WORDS));
  return capitalize(words.join(' ')) + '.';
}

function generateParagraph(sentenceCount = null) {
  const count = sentenceCount ?? Math.floor(Math.random() * 4) + 3;
  return Array.from({ length: count }, () => generateSentence()).join(' ');
}

function generateLorem(paragraphs = 1) {
  return Array.from({ length: paragraphs }, () => generateParagraph()).join('\n\n');
}

// Usage:
console.log(generateLorem(2));

React hook for placeholder text

import { useState, useCallback } from 'react';
import { LoremIpsum } from 'lorem-ipsum';

const lorem = new LoremIpsum();

function useLoremIpsum() {
  const [text, setText] = useState(() => lorem.generateParagraphs(1));

  const regenerate = useCallback((paragraphs = 1) => {
    setText(lorem.generateParagraphs(paragraphs));
  }, []);

  return { text, regenerate };
}

// Usage in a component:
function MockArticle() {
  const { text, regenerate } = useLoremIpsum();

  return (
    <article>
      <h2>{lorem.generateWords(5).split(' ').map(capitalize).join(' ')}</h2>
      <p>{text}</p>
      <button onClick={() => regenerate(2)}>Regenerate</button>
    </article>
  );
}

TypeScript: typed lorem ipsum factory

interface LoremOptions {
  paragraphs?: number;
  wordsPerSentence?: { min: number; max: number };
  sentences?: number;
}

class LoremFactory {
  private readonly words: string[];

  constructor(words: string[] = LOREM_WORDS) {
    this.words = words;
  }

  words(count: number): string {
    return Array.from({ length: count }, () =>
      this.words[Math.floor(Math.random() * this.words.length)]
    ).join(' ');
  }

  sentence(minWords = 6, maxWords = 18): string {
    const count = Math.floor(Math.random() * (maxWords - minWords)) + minWords;
    const wordList = Array.from({ length: count }, () =>
      this.words[Math.floor(Math.random() * this.words.length)]
    );
    return capitalize(wordList.join(' ')) + '.';
  }

  paragraph(minSentences = 3, maxSentences = 7): string {
    const count = Math.floor(Math.random() * (maxSentences - minSentences)) + minSentences;
    return Array.from({ length: count }, () => this.sentence()).join(' ');
  }

  generate(options: LoremOptions = {}): string {
    const { paragraphs = 1 } = options;
    return Array.from({ length: paragraphs }, () => this.paragraph()).join('\n\n');
  }
}

const factory = new LoremFactory();
const text = factory.generate({ paragraphs: 3 });

Node.js: write lorem ipsum to files

import { writeFileSync } from 'fs';
import { LoremIpsum } from 'lorem-ipsum';

const lorem = new LoremIpsum();

// Generate sample blog post JSON
const samplePosts = Array.from({ length: 10 }, (_, i) => ({
  id: i + 1,
  title: lorem.generateWords(6).split(' ').map(w => w[0].toUpperCase() + w.slice(1)).join(' '),
  excerpt: lorem.generateSentences(2),
  body: lorem.generateParagraphs(4),
  author: 'John Doe',
  publishedAt: new Date(Date.now() - i * 86400000).toISOString(),
}));

writeFileSync('sample-posts.json', JSON.stringify(samplePosts, null, 2));
console.log('Generated 10 sample posts');

Related posts

Related tool

Lorem Ipsum Generator

Generate placeholder text — words, sentences, or paragraphs. Classic lorem ipsum plus alternatives (hipster, cupcake, pirate). HTML-wrapped output option.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.