Three Ways to Title Case a Sentence in JavaScript
1. Title Case a Sentence With a FOR Loop

For this solution, we will use the String.prototype.toLowerCase() method, the String.prototype.split() method, the String.prototype.charAt() method, the String.prototype.slice() method and the Array.prototype.join() method.

The toLowerCase() method returns the calling string value converted to lowercase

method returns the calling string value converted to lowercase The split() method splits a String object into an array of strings by separating the string into substrings.

method splits a String object into an array of strings by separating the string into substrings. The charAt() method returns the specified character from a string.

method returns the specified character from a string. The slice() method extracts a section of a string and returns a new string.

method extracts a section of a string and returns a new string. The join() method joins all elements of an array into a string.

We will need to add an empty space between the parenthesis of the split()method,

var strSplit = "I'm a little tea pot".split(' ');

which will output an array of separated words:

var strSplit = ["I'm", "a", "little", "tea", "pot"];

If you don’t add the space in the parenthesis, you will have this output:

var strSplit =

["I", "'", "m", " ", "a", " ", "l", "i", "t", "t", "l", "e", " ", "t", "e", "a", " ", "p", "o", "t"];

We will concatenate

str[i].charAt(0).toUpperCase()

— which will uppercase the index 0 character of the current string in the FOR loop —

and

str[i].slice(1)

— which will extract from index 1 to the end of the string.

We will set the whole string to lower case for normalization purposes.

With comments:

Without comments:
['Sonya Moisset']
['Technology', 'JavaScript', 'Learning', 'Algorithms', 'Programming']