const capitalize = ([firstLetter, ...restOfWord]) =>
firstLetter.toUpperCase() + restOfWord.join('')
function capitalize (string) {
return string[0].toUpperCase() + string.slice(1)
}
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('')
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 index
return 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)
}
Hear me out - most JavaScript newsletters suck. That's why we made Bytes.
The goal was to create a JavaScript newsletter that was both insightful and entertaining. Over 80,000 subscribers later and well, reviews don't lie
I pinky promise you'll love it, but here's a recent issue so you can decide for yourself.
Delivered to over 80,000 developers every Monday