TypeScript - Capitalize

Capitalize

1
2
3
4
const capitalize = (s) => {
if (typeof s !== 'string') return ''
return s.charAt(0).toUpperCase() + s.slice(1)
}

This created a function called capitalize, what you pass into it (hopefully a string) will be named ‘s’ and then passed into the {} function. That function checks the data type to confirm it’s a string, and then it processes the string by capitalizing the first letter, and concatinating the rest of the string.

Samples

1
2
3
4
capitalize('flavio') //'Flavio'
capitalize('f') //'F'
capitalize(0) //''
capitalize({}) //''

References