본문 바로가기
✘✘✘ Javascript

Capitalize first letter

by PrettyLog 2023. 1. 9.
## Capitalize first letter

**[How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript)**[Ask Question](https://stackoverflow.com/questions/ask)

- simple fastest

```jsx
function capitalizeFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}

console.log(capitalizeFirstLetter('foo')); // Foo
```

- with unicode:: you can use `toLocaleUpperCase` to get locale-correct uppercasing:

```jsx
const capitalizeFirstLetter = ([ first, ...rest ], locale = navigator.language) =>
  first === undefined ? '' : first.toLocaleUpperCase(locale) + rest.join('')

console.log(
  capitalizeFirstLetter(''), // [empty string]
  capitalizeFirstLetter('foo'), // Foo
  capitalizeFirstLetter("𐐶𐐲𐑌𐐼𐐲𐑉"), // "𐐎𐐲𐑌𐐼𐐲𐑉" (correct!)
  capitalizeFirstLetter("italya", 'tr') // İtalya" (correct in Turkish Latin!)
)
```

댓글