JavaScript(JS) JS convert the first letter of a string into uppercase

www.igift‮.aedi‬com

In JavaScript, you can convert the first letter of a string into uppercase by using the toUpperCase() and slice() methods.

Here's an example:

const str = 'hello, world!';
const strCapitalized = str.charAt(0).toUpperCase() + str.slice(1);
console.log(strCapitalized); // Output: 'Hello, world!'

In this example, we first use the charAt() method to get the first character of the string ('h'), convert it to uppercase using the toUpperCase() method, and then concatenate it with the rest of the string starting from the second character, which we extract using the slice() method. Finally, we assign the result to a new variable strCapitalized.

Note that the charAt() method is used instead of the square bracket notation (str[0]) to ensure compatibility with older versions of Internet Explorer.