Capitalize Words

easy
By FrontendAtlas Editorial · Updated Oct 10, 2026
Capitalize the first letter of every word while preserving spacing. Handle multiple spaces and punctuation without collapsing the original string, and keep non-letter characters intact. This tests string parsing and careful output formatting. Concepts: strings, transforms, arrays, iteration.

Arguments

  • sentence: string — A sentence consisting of one or more words separated by spaces. May contain leading, trailing, or multiple spaces between words.

Returns

string — A new string where each word starts with an uppercase letter and the rest of the word is lowercase. Returns '' for an empty string.

Solution idea

Goal: ensure each word in a sentence starts with an uppercase letter while preserving spacing and keeping the rest of the letters lowercase. If the input is empty, return ''. This builds practical mastery over string splitting, mapping, joining, and basic character processing. Key concepts: strings, transforms, arrays.

Examples
// Basic examples

capitalizeWords('hello world') // => 'Hello World'

capitalizeWords('javascript is fun') // => 'Javascript Is Fun'

// Handles extra spaces

capitalizeWords('  welcome   to  earth  ') // => '  Welcome   To  Earth  '

// Empty string

capitalizeWords('') // => ''