Back to Snippets
CODE
Convert String to CamelCase
Description
Converts kebab-case, snake_case, or space-separated strings to camelCase
Code
/**
* Convert string to camelCase
* @param str String to convert
* @returns camelCase string
*/
function toCamelCase(str: string): string {
return str.replace(/[-_s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
}
// Usage Example
console.log(toCamelCase('hello-world')); // helloWorld
console.log(toCamelCase('hello_world')); // helloWorld
console.log(toCamelCase('hello world')); // helloWorldUsage
Useful for converting API response keys to JavaScript conventions, generating variable names, or processing user input