Convert Text to camelCase
Learn how to convert text to camelCase for JavaScript, TypeScript, and Java variable naming. Understand camelCase rules, word boundaries, and when to use it vs. other naming conventions.
Detailed Explanation
Converting Text to camelCase
camelCase joins multiple words together, removing all spaces and delimiters, capitalizing the first letter of each word except the first. It gets its name from the "humps" created by the capital letters in the middle of the identifier.
Basic Conversion
Input: user first name
Output: userFirstName
Input: get-api-response
Output: getApiResponse
Input: MAX_RETRY_COUNT
Output: maxRetryCount
How Word Boundaries Are Detected
A good camelCase converter recognizes multiple types of word boundaries:
- Spaces:
"hello world"→helloWorld - Hyphens:
"background-color"→backgroundColor - Underscores:
"created_at"→createdAt - Case transitions:
"XMLParser"→xmlParser(uppercase runs followed by lowercase) - Dots:
"my.variable.name"→myVariableName
Where camelCase Is Used
camelCase is the dominant naming convention in several ecosystems:
// JavaScript / TypeScript — variables, functions, methods
const userName = "Alice";
function getUserProfile(userId) { /* ... */ }
element.addEventListener("click", handleClick);
// Java — variables, methods, parameters
String firstName = "Alice";
public void setUserName(String name) { /* ... */ }
// JSON keys (by convention)
{ "firstName": "Alice", "lastName": "Smith" }
camelCase vs. PascalCase
The difference is simple: camelCase starts with a lowercase letter, PascalCase starts with an uppercase letter. In JavaScript/TypeScript, the convention is:
- camelCase for variables, functions, methods, and properties
- PascalCase for classes, components, and types
Handling Acronyms
Acronyms in camelCase are debatable. Two common approaches:
"XML HTTP request" → xmlHttpRequest (preferred — treat acronym as a word)
"XML HTTP request" → xmlHTTPRequest (less common — preserve acronym)
Most style guides (Google, Airbnb) recommend lowering acronyms to improve readability.
Edge Cases
- Single word:
"hello"→"hello"(no change needed). - Already camelCase: the input should pass through unchanged.
- Numbers:
"item 2 count"→"item2Count". - Leading/trailing delimiters:
"--my-var--"→"myVar".
Use Case
camelCase is the standard for JavaScript, TypeScript, and Java variable and function names. It is also the convention for JSON API keys, CSS-in-JS property names, and frontend framework props (React, Vue, Angular). Converting to camelCase is essential when mapping between different naming conventions, such as converting database column names (snake_case) to JavaScript object properties.