0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Convert String to Title Case

Use str.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()).join(' ') to convert a string to title case in JavaScript.
📋

Examples

Inputhello world
OutputHello World
InputjavaScript is fun
OutputJavascript Is Fun
Input multiple spaces here
OutputMultiple Spaces Here
🧠

How to Think About It

To convert a string to title case, split the string into words by spaces, then for each word, change the first letter to uppercase and the rest to lowercase, and finally join the words back with spaces.
📐

Algorithm

1
Get the input string.
2
Split the string into an array of words using spaces.
3
For each word, convert the first character to uppercase and the rest to lowercase.
4
Join all the transformed words back into a single string with spaces.
5
Return the resulting title-cased string.
💻

Code

javascript
function toTitleCase(str) {
  return str
    .split(' ')
    .filter(word => word.length > 0)
    .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
    .join(' ');
}

console.log(toTitleCase('hello world'));
console.log(toTitleCase('javaScript is fun'));
console.log(toTitleCase('  multiple   spaces here '));
Output
Hello World Javascript Is Fun Multiple Spaces Here
🔍

Dry Run

Let's trace 'javaScript is fun' through the code

1

Split string

Split 'javaScript is fun' into ['javaScript', 'is', 'fun']

2

Filter empty words

No empty words to remove, array remains ['javaScript', 'is', 'fun']

3

Map each word

Transform each word: 'javaScript' -> 'Javascript' 'is' -> 'Is' 'fun' -> 'Fun'

4

Join words

Join ['Javascript', 'Is', 'Fun'] into 'Javascript Is Fun'

Original WordTransformed Word
javaScriptJavascript
isIs
funFun
💡

Why This Works

Step 1: Splitting the string

We split the string by spaces using split(' ') to get each word separately.

Step 2: Capitalizing each word

For each word, we take the first letter with charAt(0), convert it to uppercase, then add the rest of the word in lowercase with slice(1).toLowerCase().

Step 3: Joining words back

We join all the capitalized words back into one string with spaces using join(' ').

🔄

Alternative Approaches

Using Regular Expression
javascript
function toTitleCaseRegex(str) {
  return str.toLowerCase().replace(/\b\w/g, char => char.toUpperCase());
}

console.log(toTitleCaseRegex('hello world'));
This method uses regex to find word starts and capitalize them, which is concise but less explicit.
Using for loop
javascript
function toTitleCaseLoop(str) {
  const words = str.split(' ').filter(w => w.length > 0);
  let result = '';
  for (let i = 0; i < words.length; i++) {
    result += words[i].charAt(0).toUpperCase() + words[i].slice(1).toLowerCase();
    if (i < words.length - 1) result += ' ';
  }
  return result;
}

console.log(toTitleCaseLoop('hello world'));
This approach uses a loop instead of map, which some beginners find easier to understand.

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

Time Complexity

The program processes each character once when splitting and mapping, so time grows linearly with string length.

Space Complexity

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

Which Approach is Fastest?

The regex method is concise and fast for typical strings, but the map method is clearer and easier to maintain.

ApproachTimeSpaceBest For
Map with splitO(n)O(n)Readability and clarity
Regex replaceO(n)O(n)Concise code and speed
For loopO(n)O(n)Beginners learning loops
💡
Always convert the rest of the word to lowercase to handle mixed-case inputs correctly.
⚠️
Forgetting to lowercase the rest of the word causes inconsistent capitalization in the output.