Capitalize string
17 March 2022 (Updated 7 April 2022)
You want a function that takes a string and returns the string with the first character in uppercase and the rest in lowercase:
capitalize('john doe') // John Doe
capitalize('JOHN DOE') // John Doe
Here’s how in JavaScript:
function capitalize(str) {
return str.slice(0, 1).toUpperCase() + str.slice(1).toLowerCase()
}
Tagged:
Algorithms