JavaScript example
Convert snake_case to camelCase in JavaScript
For predictable snake_case input, split on underscores, normalize each token, keep the first token lowercase, and capitalize later tokens. The helper also tolerates repeated, leading, and trailing underscores.
JavaScript code
function snakeToCamel(value) {
const parts = value.trim().replace(/^_+|_+$/g, "").split(/_+/).filter(Boolean)
if (parts.length === 0) return ""
return parts[0].toLowerCase() + parts.slice(1).map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("")
}
snakeToCamel("user_profile_image")
// "userProfileImage"Trim whitespace, remove leading or trailing underscores, and split on one or more underscores so repeated separators do not create empty middle tokens.
This is appropriate for genuine snake_case. If input can contain PascalCase, acronyms, spaces, dashes, or mixed separators, use a broader tokenizer.
Edge cases to consider
- Acronyms such as API and URL may require project-specific casing.
- Mixed input such as user_profile-URL needs broader tokenization.
- Recursively converting object keys is different from converting one string.
- Generated variable names may need reserved-word and identifier validation.
Example
| Before | After |
|---|---|
| user_profile_image | userProfileImage |
| __user__profile__ | userProfile |
| api_response_url | apiResponseUrl |
| created_at | createdAt |
JavaScript code
const cases = [
["user_profile_image", "userProfileImage"],
["__user__profile__", "userProfile"],
["api_response_url", "apiResponseUrl"],
["created_at", "createdAt"],
]
for (const [input, expected] of cases) {
console.assert(snakeToCamel(input) === expected, input)
}Try the conversion online
Use the browser-based converter for quick checks, batch values, acronym handling, or inputs that mix several naming conventions.
Detected: snake_case
camelCase output
userProfileImageUrl
All naming formats
camelCase
userProfileImageUrlPascalCase
UserProfileImageUrlsnake_case
user_profile_image_urlkebab-case
user-profile-image-urlCONSTANT_CASE
USER_PROFILE_IMAGE_URLdot.case
user.profile.image.urlpath/case
user/profile/image/urlConversion happens locally in your browser. Your input is not sent to a conversion server.