DEV Community

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

Collapse
 
josemunoz profile image
José Muñoz

here's my take on it with ES7

const capitalize = ([initial, ...rest]) => `${initial.toUpperCase()}${rest.join('').toLowerCase()}`;

a little cleaner

const capitalize = input => {
  const [initial, ...rest] = input;
  const capitalizedInitial = initial.toUpperCase();
  const loweredBody = rest.join('').toLowerCase();

  return `${capitalizedInitial}${loweredBody}`;
};
Collapse
 
samanthaming profile image
Samantha Ming

One-liner solutions are most of the time my favs too! 😜

Collapse
 
kurisutofu profile image
kurisutofu • Edited
const [initial, ...rest] = input;

I didn't think of it that way. I like it!