Capitalize first letter: safe against empty string

`""[0]` -> `undefined`
`"".slice(0, 1)` -> `""`
This commit is contained in:
Ivan Babak
2017-12-11 18:29:37 -08:00
committed by GitHub
parent 62f3ffddc2
commit 649d1c3ae0

View File

@ -3,5 +3,5 @@
Use `toUpperCase()` to capitalize first letter, `slice(1)` to get the rest of the string.
```js
var capitalize = str => str[0].toUpperCase() + str.slice(1);
var capitalize = str => str.slice(0, 1).toUpperCase() + str.slice(1);
```