To capitalize the first letter of a word in JavaScript, you can use a simple function. Here’s an example:
function capitalizeFirstLetter(word) {
return word.charAt(0).toUpperCase() + word.slice(1);
}
// Example usage
const originalWord = 'hello';
const capitalizedWord = capitalizeFirstLetter(originalWord);
console.log(capitalizedWord); // Output: Hello
In the capitalizeFirstLetter
function, we take the input word as a parameter. We use the charAt(0)
method to extract the first character of the word and then use the toUpperCase
method to convert it to uppercase. The slice(1)
method is used to retrieve the rest of the word (from the second character onwards) and keep it unchanged. Finally, we concatenate the capitalized first letter with the remaining word and return the result.
You can call the capitalizeFirstLetter
function with any word, and it will return the word with the first letter capitalized.