Capitalize / Title Case String
Capitalize / Title Case String is a reusable JavaScript snippet. Transforms strings to various capitalization formats: uppercase first letter, title case, camelCase, and snake_case. The full implementation: // Capitalize first letter const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1).toLowerCase() // Title case const titleCase = str => str.toLowerCase().split(' ') .map(w => w.charAt(0).toUpperCase() + w.slice(1)) .join(' ') // Camel case const camelCase = str => str.toLowerCase() .replace(/[_-\s]+(.)/g, (_, c) => c.toUpperCase()) // Snake case const snakeCase = str => str.replace(/[A-Z]/g, c => '_' + c.toLowerCase()) .replace(/^_/, '') .replace(/[\s-]+/g, '_') // Usage capitalize('hello world') // "Hello world" titleCase('hello world') // "Hello World" camelCase('hello_world') // "helloWorld" snakeCase('helloWorld') // "hello_world" This snippet uses only standard JavaScript with no dependencies, so it runs in modern browsers and Node.js alike. Browse more patterns in the snippet library, or paste the code straight into the playground to experiment with it.