How do I make the first letter of a string uppercase, but not change the case of any of the other letters?
For example:
\"this is a test\"
Here is a shortened version of the popular answer that gets the first letter by treating the string as an array:
function capitalize(s)
{
return s[0].toUpperCase() + s.slice(1);
}
Update:
According to the comments below this doesn't work in IE 7 or below.
Update 2:
To avoid undefined
for empty strings (see @njzk2's comment below), you can check for an empty string:
function capitalize(s)
{
return s && s[0].toUpperCase() + s.slice(1);
}