No Time to Read - Copy/Paste - ES6 Solution
const capitalize = ([firstLetter, ...restOfWord]) =>firstLetter.toUpperCase() + restOfWord.join("");
No Time to Read - Copy/Paste - ES5 Solution
function capitalize(string) {return string[0].toUpperCase() + string.slice(1);}
ES6 Explanation
The first thing we want to do is take the string we're trying to capitalize and separate it into two pieces, the first letter, and everything else. To accomplish this we'll use an ES6 feature called Array Destructuring (which conveniently works on strings as well).
If you're not familiar with Array Destructuring, check out Object and Array Destructuring in JavaScript
Just as if we were to destructure the string in the body of the function, we can also do it in the function's parameter.
const capitalize = ([firstLetter, ...restOfWord]) => {};
At this point we have two variables local to our capitalize
function - firstLetter
and restOfWord
. firstLetter
is, well, the first letter of the string argument that was passed into capitalize
. restOfWord
is an array that contains all of the other characters of the argument.
const capitalize = ([firstLetter, ...restOfWord]) => {console.log(firstLetter); // 'h'console.log(restOfWord); // ["e", "l", "l", "o"]};
Now that we've separated our string into the first letter and everything else, the next step is to capitalize the first letter. Lucky for us, all JavaScript strings have access to a .toUpperCase
method which we can use.
const capitalize = ([firstLetter, ...restOfWord]) => {const capitalizedFirstLetter = firstLetter.toUpperCase();};
The final step is to take the capitalized first letter, combine it with the rest of the word, and return the result.
Remember at this point we have two variables, capitalizedFirstLetter
which is a string and restOfWord
which is an array. If we convert restOfWord
back to a string then we can just return the result of adding capitalizedFirstLetter
and the string form of restOfWord
back together.
To convert an array into a string, we can use the .join
method.
const capitalize = ([firstLetter, ...restOfWord]) => {const capitalizedFirstLetter = firstLetter.toUpperCase();const restOfWordString = restOfWord.join("");};
Now that we have the capitalized first letter and the rest of the word as a string, we can add them together and return the result.
const capitalize = ([firstLetter, ...restOfWord]) => {const capitalizedFirstLetter = firstLetter.toUpperCase();const restOfWordString = restOfWord.join("");return capitalizedFirstLetter + restOfWordString;};
Now to get to our final solution, we can get rid of the variables and use Arrow Function's implicit return.
const capitalize = ([firstLetter, ...restOfWord]) =>firstLetter.toUpperCase() + restOfWord.join("");
ES5 Explanation
The ES5 solution follows the same logic as the ES6 solution. We first want to separate the first letter from the rest of the word. In ES5, we can grab the first letter by using bracket notation at the 0
index and we can get the rest of the word by using JavaScript's .slice
method.
function capitalize(string) {var firstLetter = string[0];var restOfWord = string.slice(1); // start at the 1 index}
Now that we've separated the first letter from the rest of the word, we want to capitalize the first letter then combine it with the rest of the word.
function capitalize(string) {var firstLetter = string[0];var restOfWord = string.slice(1); // start at the 1 indexreturn firstLetter.toUpperCase() + restOfWord;}
And finally, to get to our final solution we can get rid of the variable and do everything in line.
function capitalize(string) {return string[0].toUpperCase() + string.slice(1);}
Before you leave
I know, "another newsletter pitch" - but hear me out. Most JavaScript newsletters are terrible. When's the last time you actually looked forward to getting one? Even worse, when's the last time you actually read one rather than just skim it?
We wanted to change that, which is why we created Bytes. The goal was to create a JavaScript newsletter that was both educational and entertaining. 101,890 subscribers and an almost 50% weekly open rate later, it looks like we did it.
Delivered to 101,890 developers every Monday

Sdu
@sduduzo_g
This is the first ever newsletter that I open a music playlist for and maximize my browser window just to read it in peace. Kudos to @uidotdev for great weekly content.

Brandon Bayer
@flybayer
The Bytes newsletter is a work of art! It's the only dev newsletter I'm subscribed too. They somehow take semi boring stuff and infuse it with just the right amount of comedy to make you chuckle.

John Hawley
@johnhawly
Bytes has been my favorite newsletter since its inception. It's my favorite thing I look forward to on Mondays. Goes great with a hot cup of coffee!

Garrett Green
@garrettgreen
I subscribe to A LOT of dev (especially JS/TS/Node) newsletters and Bytes by @uidotdev is always such a welcomed, enjoyable change of pace to most (funny, lighthearted, etc) but still comprehensive/useful.

Muhammad
@mhashim6_
Literally the only newsletter I’m waiting for every week.

Grayson Hicks
@graysonhicks
Bytes is the developer newsletter I most look forward to each week. Great balance of content and context! Thanks @uidotdev.

Mitchell Wright
@mitchellbwright
I know I've said it before, but @tylermcginnis doesn't miss with the Bytes email. If you're a developer, you really need to subscribe

Ali Spittel
@aspittel
Can I just say that I giggle every time I get the @uidotdev email each week? You should definitely subscribe.

Chris Finn
@thefinnomenon
Every JavaScript programmer should be subscribed to the newsletter from @uidotdev. Not only do they manage to succinctly cover the hot news in the JavaScript world for the week but it they manage to add a refreshing humor to it all.