Developer Documentation
JavaScript
Updated September 19, 2026

Convert Text Case in JavaScript

Convert words between sentence, title, camel, Pascal, snake, kebab, and constant case in JavaScript.

Case conversion looks simple until punctuation, acronyms, existing camelCase words, Unicode, and line breaks appear. Normalize text into tokens first, then render those tokens into the requested case.

Quick Start#

The following example uses the standard JavaScript API or a widely used package for this task.

Tokenize then render#

JavaScript
1function words(input) {2  return input3    .replace(/([a-z0-])([A-Z])/g, "$1 $2")4    .trim()5    .split(/[^\p{L}\p{N}]+/u)6    .filter(Boolean)7    .map((word) => word.toLocaleLowerCase());8}910const capitalize = (word) => word ? word[].toLocaleUpperCase() + word.slice() : word;1112const toCamel = (input) => {13  const parts = words(input);14  return parts.map((w, i) => i ===  ? w : capitalize(w)).join("");15};1617const toSnake = (input) => words(input).join("_");18const toKebab = (input) => words(input).join("-");

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Pascal and constant case#

JavaScript
1const toPascal = (input) => words(input).map(capitalize).join("");2const toConstant = (input) => words(input).join("_").toLocaleUpperCase();34console.log(toPascal("hello toolPorta"));5console.log(toConstant("hello toolPorta"));

Important Notes#

Identifier case conversion and editorial Title Case are different problems. Define a title-style policy instead of calling a simple capitalization algorithm APA or Chicago style.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Text Case in other languages: