DEV Community

Discussion on: Web Basics: How to Capitalize a Word in JavaScript

Collapse
 
qm3ster profile image
Mihail Malo • Edited

You can omit the second argument in substring, substr and slice to get the rest of the string until the end.
You can also use charAt(0) or even .charAt() to get the first char instead of a slicing method.

const capitalize = str => str
  .toLowerCase()
  .split(' ')
  .map(strWord => strWord.charAt().toUpperCase() + strWord.slice(1))
  .join(' ')