OneCompiler

[Javascript] How can I make the first letter of a string to uppercase?

I want to make the first letter of a string alone to uppercase. How can I do that in JavaScript?

1 Answer

4 years ago by

Below code shows you how to make the first letter of a string to uppercase and also first letter of each word of the string to uppercase.

let str ='hello world';

// to make the first letter of the string to uppercase.
console.log(str[0].toUpperCase() + str.slice(1));

// to make the first letter of each word of the string to uppercase.
console.log(str.split(' ').map(ele=>ele[0].toUpperCase()+ele.slice(1)).join(' '))

Run here https://onecompiler.com/javascript/3xns8dsr3

4 years ago by Meera