Convert String to Lower Case :
In lower case, all letters of string appears in their small forms. Using the inbuilt toLowerCase()
method we can easily convert given text to lower case letters with JavaScript.
var str = "JavaScript is Awesome";
console.log(str.toLowerCase());
// Output: javascript is awesome
Convert String to Upper Case :
In upper case, all letters of string appears in their capital forms Using the inbuilt toUpperCase()
method a string can be converted to upper case letters with JavaScript.
var str = "JavaScript is Awesome";
console.log(str.toUpperCase());
// Output: JAVASCRIPT IS AWESOME
Convert String to Title Case :
In title case, first letter of every word in the string appears in capital form and rest of the letters remain in small form. There is no inbuilt method to convert a string to title case in JavaScript. But we can do this in various ways. I will show you one simple way, checkout the following example:
function toTitleCase(str) {
// Validate input
if(!str) return;
// Split the string into array of words
var wordsArr = str.split(' ');
var result = [];
var word = "";
for(var i = 0; i < wordsArr.length; i++){
// Lower case each word
word = wordsArr[i].toLowerCase();
// Convert first character to upper case, append the rest
result.push(word[0].toUpperCase() + word.slice(1));
}
// Convert array of title cased words to space separated string
return result.join(' ');
}
var str = "JavaScript is Awesome";
var titleCased = toTitleCase(str);
console.log(titleCased);
// Output: Javascript Is Awesome
The function is self explanatory. I am using inbuilt array methods split()
, join()
and splice()
to convert given string to title case. If you want to learn more about the array methods read this article.