Function that takes any string and returns it with the 1st character as upper-case?

Answered
suparman21 asked this question 10 months ago
suparman21 on Jan 19 · Edited

How do I create a function that takes any string and returns the string with the first character in upper case format?

Thanks in advance!

2 suggested answers
moon_man41 on Jan 21

Try this one out:

function capitalize(s) {
  return s[0].toUpperCase() + s.slice(1)
}
0 replies
yaboy01 on Jan 21
function uppercaseFirstLetter(string) {
  return string.charAt(0).toUpperCase() + string.slice(1)
}

string.charAt(0).toUpperCase() takes the first letter from the given string parameter and converts it to upper case. Then, string.slice(1) adds the rest of the string on to it.

Here's how you'd use it:

uppercaseFirstLetter("tacos") // returns "Tacos"
0 replies
Answered