letter

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

隐身守侯 提交于 2019-11-25 23:56:31
问题 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\" -> \"This is a test\" \"the Eiffel Tower\" -> \"The Eiffel Tower\" \"/index.html\" -> \"/index.html\" 回答1: function capitalizeFirstLetter(string) { return string.charAt(0).toUpperCase() + string.slice(1); } Some other answers modify String.prototype (this answer used to as well), but I would advise against this now due to maintainability (hard to find out