JavaScript function to capitalize first letter of word

Post author: Adam VanBuskirk
Adam VanBuskirk
6/18/23 in
Tech
JavaScript

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.

Sign up today for our weekly newsletter about AI, SEO, and Entrepreneurship

Leave a Reply

Your email address will not be published. Required fields are marked *


Read Next




© 2024 Menyu LLC