Capitalize Words

Capitalizes the first letter of each word in a string.

Contributed by @itsbrunodev

javascript
function capitalizeWords(str) {
  return str.replace(/\b\w/g, (char) => char.toUpperCase());
}
javascript
capitalizeWords("hello world"); // "Hello World"

Keywords

string
casing