Files
30-seconds-of-code/snippets/capitalize-first-letter.md
Angelos Chalaris d7856f0b40 Resolves #16
2017-12-12 15:42:03 +02:00

400 B

Capitalize first letter

Use slice(0,1) and toUpperCase() to capitalize first letter, slice(1) to get the rest of the string. Omit the lowerRest parameter to keep the rest of the string intact, or set it to true to convert to lower case.

const capitalize = (str, lowerRest = false) =>
  str.slice(0, 1).toUpperCase() + (lowerRest? str.slice(1).toLowerCase() : str.slice(1));