How do I make the first letter of a string uppercase in JavaScript?

Created by Steve HarrisonLinked to 74.4m issues across 167 teams

tl;dr

To make the first letter of a string uppercase in JavaScript, you can use the following function:

function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); }

This function takes a string as an argument and returns the same string with the first letter capitalized. It is important to note that this function does not modify the String.prototype, which can cause maintainability issues if other code uses the same name or if a browser adds a native function with the same name in the future.