# Capitalizing first letter of each word in a string

**URL:** <https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [September 7, 2023, 8:51am UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792 "2023-09-07T08:51:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![okundin](https://avatars.discourse-cdn.com/v4/letter/o/5daacb/32.png) [@okundin](https://es.discourse.group/u/okundin)\
**Post date:** [September 7, 2023, 8:51am UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/1 "2023-09-07T08:51:14Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [September 7, 2023, 11:56am UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/2 "2023-09-07T11:56:13Z")

</div>

Welcome!

This looks like a duplicate of [Proposal: String.prototype.capitalize](https://es.discourse.group/t/proposal-string-prototype-capitalize/1662)

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [September 7, 2023, 2:33pm UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/3 "2023-09-07T14:33:42Z")

</div>

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

```javascript
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.

---

<div class="post-metadata">

**Author:** ![okundin](https://avatars.discourse-cdn.com/v4/letter/o/5daacb/32.png) [@okundin](https://es.discourse.group/u/okundin)\
**Post date:** [September 11, 2023, 5:43pm UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/4 "2023-09-11T17:43:05Z")

</div>

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 :).

---

<div class="post-metadata">

**Author:** ![okundin](https://avatars.discourse-cdn.com/v4/letter/o/5daacb/32.png) [@okundin](https://es.discourse.group/u/okundin)\
**Post date:** [September 11, 2023, 5:46pm UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/5 "2023-09-11T17:46:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![theScottyJam](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/thescottyjam/32/616_2.png) [@theScottyJam](https://es.discourse.group/u/theScottyJam)\
**Post date:** [September 12, 2023, 1:32am UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/6 "2023-09-12T01:32:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [October 10, 2023, 11:57pm UTC](https://es.discourse.group/t/capitalizing-first-letter-of-each-word-in-a-string/1792/7 "2023-10-10T23:57:04Z")

</div>

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
