Javascript Capitalize First Letter
To capitalize the first letter of a string in JavaScript, you can use this simple function:
✅ Example:
📌 How it works:
-
str.charAt(0).toUpperCase()→ Capitalizes the first character. -
str.slice(1)→ Gets the rest of the string starting from the second character. -
The two parts are concatenated.
To capitalize the first letter of each word in a string
✅ Example:
console.log(capitalizeEachWord(“hello world”));
// Output: “Hello World”
console.log(capitalizeEachWord(“javaScript is awesome”));
// Output: “Javascript Is Awesome”
🔍 How it works:
-
split(' ')→ Splits the string into an array of words. -
map(...)→ Capitalizes the first letter of each word. -
join(' ')→ Joins the capitalized words back into a single string.