Capitalizing first letter of each word in a string

Hi everyone,

I am newby here and in programming, who started with Python. When I started studying JavaScript, I learned, while doing excercises, that there is no easy way to capitalize first letter of each word in a string.

I found this solution on the web, that allows to do it with vanilla JS:

const firstName = 'andrey';
const lastName = 'kudinov';
console.log(${firstName.charAt(0).toUpperCase() + firstName.slice(1)} ${lastName.charAt(0).toUpperCase()}${lastName.slice(1)});

This code is good for two variables. If there is just one variable containing string 'andrey kudinov', then it would be even more complex task since I don't know index of the second word (users have dffferent name lengths).

Now how I capitalize first letter of each word in Python:

username = 'andrey kudinov'
print(username.title())
THAT'S IT!

I think that there are many instances where there is a need to address to a user. And it would be very helpful to have this kind of method in vanilla JavaScript to simplify developers' work.

If there is a simple way to do it in JS (similar to Python) aleardy in existence, I would be happy to see it. So far, I have not found anything as simple as in Python.

Welcome!

This looks like a duplicate of Proposal: String.prototype.capitalize

1 Like

A quick-and-dirty way to do it for now is as follows:

function title(str) {
  return str.split(' ')
    .map(word => word[0].toUpperCase() + word.slice(1))
    .join(' ')
}

const name = 'andrey kudinov'
console.log(title(name)) // 'Andrey Kudinov'

But, as noted in the thread linked above, this does not work properly for many types of sentences, for example, it won't leave words like "the" in lowercase like a proper title-casing should do. Python's .title() is incorrectly implemented and fails to handle these language nuances as well.

1 Like

Totally support that idea. It's just written a bit more complex than mine simplified suggestion. I thought it was something different related to advanced level :).

Thanks for the in-depth explanation. But I guess my idea should simply be applied to limited cases, specifically working with usernames, book titles and similar cases where a developer needs to make sure that there is no duplicate in a database records.

Could you expound on these use-cases, I'm not sure I follow. If you want to make sure there's no duplicates in the database, you usually can perform a case-insensitive search.

Is this what you're looking for?

  • First word: value.replace(/^\p{Lu}/gu, m => m.toUpperCase())
  • Every word: value.replace(/\b\p{Lu}/gu, m => m.toUpperCase())
    • This is imprecise, especially around hyphens.

Note that this itself misses a lot, and some languages don't capitalize as much as others, and not all language even have case:

  • German capitalizes almost all nouns
  • English only capitalizes proper nouns, place names, titles applied to such nouns, and a few other smaller categories
  • Spanish generally only capitalizes proper nouns
  • Most East Asian languages (like Japanese, Chinese, and Korean) don't use sentence case at all