DEV Community

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

Collapse
 
qm3ster profile image
Mihail Malo • Edited

You can just map on the string :v

const capitalize = str =>
  Array.prototype.map
    .call(str, (c, i) => i ? c.toLowerCase() : c.toUpperCase())
    .join('')

But no, it's most likely not going to be performant (even like this), because it involves indexing into every character instead of handing the underlying native string to the native method.

Worse yet, .split('') and such don't play well with composite Unicode graphemes.