0
0
JavascriptProgramBeginner · 2 min read

JavaScript Program to Capitalize First Letter of Each Word

Use str.split(' ').map(word => word.charAt(0).toUpperCase() + word.slice(1)).join(' ') to capitalize the first letter of each word in a string.
📋

Examples

Inputhello world
OutputHello World
InputjavaScript is fun
OutputJavaScript Is Fun
Input multiple spaces here
Output Multiple Spaces Here
🧠

How to Think About It

To capitalize the first letter of each word, split the string into words by spaces, then for each word, change the first letter to uppercase and keep the rest as is, 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, change the first character to uppercase and keep the rest unchanged.
4
Join all the words back into a single string with spaces.
5
Return the resulting string.
💻

Code

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

const input = "hello world";
console.log(capitalizeFirstLetter(input));
Output
Hello World
🔍

Dry Run

Let's trace the input 'hello world' through the code

1

Split string

Split 'hello world' into ['hello', 'world']

2

Capitalize each word

For 'hello', first letter 'h' to 'H', result 'Hello'; for 'world', 'w' to 'W', result 'World'

3

Join words

Join ['Hello', 'World'] into 'Hello World'

WordCapitalized Word
helloHello
worldWorld
💡

Why This Works

Step 1: Splitting the string

Splitting the string by spaces creates an array of words to process individually.

Step 2: Capitalizing first letter

For each word, charAt(0).toUpperCase() changes the first letter to uppercase, and slice(1) keeps the rest unchanged.

Step 3: Joining words back

Joining the array with spaces reconstructs the string with capitalized words.

🔄

Alternative Approaches

Using regular expression
javascript
function capitalizeWithRegex(str) {
  return str.replace(/\b\w/g, char => char.toUpperCase());
}

console.log(capitalizeWithRegex('hello world'));
This method uses regex to find word boundaries and capitalize the first letter directly, which is concise but may be less clear for beginners.
Using for loop
javascript
function capitalizeWithLoop(str) {
  const words = str.split(' ');
  for (let i = 0; i < words.length; i++) {
    if (words[i].length > 0) {
      words[i] = words[i][0].toUpperCase() + words[i].slice(1);
    }
  }
  return words.join(' ');
}

console.log(capitalizeWithLoop('hello world'));
This approach uses a classic loop for clarity and control, good for learners who prefer explicit iteration.

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

Time Complexity

The program processes each character once during splitting and mapping, so time grows linearly with input size.

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 usually fastest due to internal optimizations, but the split-map-join method is more readable.

ApproachTimeSpaceBest For
Split-Map-JoinO(n)O(n)Readability and clarity
Regular ExpressionO(n)O(n)Concise and fast for simple cases
For LoopO(n)O(n)Explicit control and beginner-friendly
💡
Always check for empty strings to avoid errors when capitalizing words.
⚠️
Forgetting to handle empty strings or multiple spaces can cause unexpected results or errors.