Javascript: ucFirst

In PHP, there is a function ucfirst whose purpose is to uppercase the first letter of the string. I've written the equivalent in Javascript :

function ucFirst(str) {
  if (str.length > 0) {
    return str[0].toUpperCase() + str.substring(1);
  } else {
    return str;
  }
}

Add new comment