- Instant help with your JavaScript coding problems

Convert a string to sentence case in JavaScript

Question:
How to convert a string to sentence case in JavaScript?
Answer:
function toSentenceCase(str){
    return str.toLowerCase().charAt(0).toUpperCase() + str.slice(1);;
}

console.log(toSentenceCase('this is a simple sentence')); // This is a simple sentence
Description:

The sentence case capitalizes only the first letter of the first word in the string, as you would when writing a sentence. To do this take the first character charAt(0) of the string and convert it to uppercase toUpperCase and then simply append the rest of the string str.slice(1) to the first already capitalized letter.

Depending on the input text 'quality' you can optionally convert the full string to lower case first, but in this case valid names, abbreviations will be also converted to lower case.

Reference:
charAt reference
Share "How to convert a string to sentence case in JavaScript?"