You can use the toTitleCase()
function to convert a string to title case in JavaScript:
const toTitleCase = (str) =>
str.replace(
/\w\S*/g,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);
Then, you can call this function on a string like this:
const myString = "this is a string";
const titleCaseString = toTitleCase(myString);
console.log(titleCaseString); // This Is A String
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.