[Home] > Snippets  > Languages  > JavaScript  > Strings  >  Convert a string to camelCase

Convert a string to camelCase

JavaScript

const toCamelCase = (str) => str.trim().replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))

TypeScript

const toCamelCase = (str: string): string => str.trim().replace(/[-_\s]+(.)?/g, (_, c) => (c ? c.toUpperCase() : ''))

Examples

toCamelCase('background-color') // backgroundColor
toCamelCase('-webkit-scrollbar-thumb') // WebkitScrollbarThumb
toCamelCase('_hello_world') // HelloWorld
toCamelCase('hello_world') // helloWorld