Skip to content

Lorem Ipsum Generator — JavaScript source

Generate placeholder Lorem Ipsum text - paragraphs and sentences with adjustable counts. Classic Latin word pool, instant copy.

This is the JavaScript implementation — the same logic the interactive tool runs, in a shareable, citable form.

/**
 * Lorem Ipsum generator - JavaScript polyglot showcase port.
 *
 * Pure structure generator: random words drawn from the canonical Lorem
 * Ipsum pool and assembled into capitalized sentences, paragraphs, and
 * multi-paragraph blocks. Mirrors the TypeScript reference implementation
 * in src/lib/lorem.ts 1:1 (same word pool, same defaults, same edge-case
 * handling, same non-crypto randomness).
 *
 * Uses ESM exports - the modern JavaScript analogue of the TypeScript
 * source's `export function` / `export const`.
 *
 * Ported from src/lib/lorem.ts.
 * Display source - part of CosmoDev's polyglot tool pages.
 */

// Canonical Lorem Ipsum word pool. Split from the exact same string as the
// TypeScript source so every port draws from an identical vocabulary.
const WORDS = (
  'lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor ' +
  'incididunt ut labore et dolore magna aliqua enim ad minim veniam quis nostrud exercitation ' +
  'ullamco laboris nisi aliquip ex ea commodo consequat duis aute irure in reprehenderit voluptate ' +
  'velit esse cillum eu fugiat nulla pariatur excepteur sint occaecat cupidatat non proident sunt ' +
  'culpa qui officia deserunt mollit anim id est laborum'
).split(' ');

/**
 * Uniform integer in [0, max). Uses Math.random() to match the TypeScript
 * source's PRNG - appropriate for placeholder text, never for security.
 */
function randInt(max) {
  return Math.floor(Math.random() * max);
}

/**
 * Build a single sentence of between `minWords` and `maxWords` words
 * (inclusive). When `maxWords < minWords` the range collapses to exactly
 * `minWords` words via the `Math.max(1, ...)` span guard.
 */
export function sentence(minWords = 8, maxWords = 16) {
  const span = Math.max(1, maxWords - minWords + 1);
  const n = minWords + randInt(span);
  const words = [];
  for (let i = 0; i < n; i++) words.push(WORDS[randInt(WORDS.length)]);
  const body = words.join(' ');
  // Capitalize only the first character; the rest of the sentence is
  // left untouched, then a period is appended.
  return body.charAt(0).toUpperCase() + body.slice(1) + '.';
}

/** Join `sentences` default-length (8..16 word) sentences into a paragraph. */
export function paragraph(sentences = 5) {
  return Array.from({ length: sentences }, () => sentence()).join(' ');
}

/** Build `paragraphs` paragraphs of `sentencesPerPara` sentences each. */
export function lorem(paragraphs = 3, sentencesPerPara = 5) {
  return Array.from({ length: paragraphs }, () =>
    paragraph(sentencesPerPara),
  ).join('\n\n');
}

export { WORDS };

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →