0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Convert String to CamelCase

Use string.toLowerCase() to make all letters small, then split the string by spaces, capitalize the first letter of each word except the first, and join them back with no spaces to get camelCase.
📋

Examples

Inputhello world
OutputhelloWorld
InputJavaScript is fun
OutputjavaScriptIsFun
Input multiple spaces here
OutputmultipleSpacesHere
🧠

How to Think About It

To convert a string to camelCase, first make the whole string lowercase so letters are uniform. Then split the string into words by spaces. Keep the first word as is, but for each next word, change its first letter to uppercase and keep the rest lowercase. Finally, join all words together without spaces to form camelCase.
📐

Algorithm

1
Get the input string.
2
Convert the entire string to lowercase.
3
Split the string into words using spaces as separators.
4
Keep the first word as lowercase.
5
For each remaining word, capitalize the first letter and keep the rest lowercase.
6
Join all words together without spaces and return the result.
💻

Code

javascript
function toCamelCase(str) {
  const words = str.toLowerCase().trim().split(/\s+/);
  return words.map((word, index) => {
    if (index === 0) return word;
    return word.charAt(0).toUpperCase() + word.slice(1);
  }).join('');
}

console.log(toCamelCase('hello world'));
console.log(toCamelCase('JavaScript is fun'));
console.log(toCamelCase('  multiple   spaces here '));
Output
helloWorld javaScriptIsFun multipleSpacesHere
🔍

Dry Run

Let's trace 'hello world' through the code

1

Convert to lowercase and trim

'hello world' -> 'hello world'

2

Split into words

['hello', 'world']

3

Map words to camelCase

Index 0: 'hello' (unchanged), Index 1: 'world' -> 'World'

4

Join words

'hello' + 'World' -> 'helloWorld'

IndexWordTransformed Word
0hellohello
1worldWorld
💡

Why This Works

Step 1: Lowercase the string

Using toLowerCase() makes all letters small so the output is consistent.

Step 2: Split into words

Splitting by spaces breaks the string into separate words to process individually.

Step 3: Capitalize words except first

The first word stays lowercase, others get their first letter uppercase using charAt(0).toUpperCase().

Step 4: Join words without spaces

Joining all words without spaces creates the camelCase format.

🔄

Alternative Approaches

Using regular expression replace
javascript
function toCamelCase(str) {
  return str.toLowerCase().replace(/(?:\s+)(.)/g, (_, c) => c.toUpperCase());
}

console.log(toCamelCase('hello world'));
This method uses regex to find spaces followed by a letter and capitalizes that letter, which is concise but less readable for beginners.
Using reduce to build string
javascript
function toCamelCase(str) {
  return str.toLowerCase().trim().split(/\s+/).reduce((acc, word, i) => {
    if (i === 0) return word;
    return acc + word.charAt(0).toUpperCase() + word.slice(1);
  }, '');
}

console.log(toCamelCase('hello world'));
Using reduce builds the camelCase string step-by-step, which is functional but slightly more complex.

Complexity: O(n) time, O(n) space

Time Complexity

The code processes each character once when converting to lowercase and splitting, so it runs in linear time relative to the string length.

Space Complexity

Extra space is used for the array of words and the output string, both proportional to input size.

Which Approach is Fastest?

The regex replace method is concise and fast for small strings, but the split-map approach is clearer and easier to maintain.

ApproachTimeSpaceBest For
Split and mapO(n)O(n)Readability and clarity
Regex replaceO(n)O(n)Concise code for small strings
Reduce methodO(n)O(n)Functional style preference
💡
Always trim the string and split by one or more spaces to handle extra spaces cleanly.
⚠️
Forgetting to lowercase the entire string first can cause inconsistent capitalization in the output.