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

前端 未结 30 2336
南方客
南方客 2020-11-21 05:00

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\"
30条回答
  •  猫巷女王i
    2020-11-21 05:39

    This is the 2018 ECMAScript 6+ Solution:

    const str = 'the Eiffel Tower';
    const newStr = `${str[0].toUpperCase()}${str.slice(1)}`;
    console.log('Original String:', str); // the Eiffel Tower
    console.log('New String:', newStr); // The Eiffel Tower

提交回复
热议问题