JavaScript upperCase - Convert String To Uppercase
In this tutorial, you will learn how to convert the case of a string to uppercase and will also look at ways to capitalize the first character of each word in a sentence.
JavaScript toUpperCase Method
The toUpperCase method is a javascript string method that converts the case of calling string to uppercase and returns it as a new string. The method does not affect the sentence, character, or string which is already in the capital.
The method does not change the original string but returns a new string after capitalisation.
Example
var sentence = "To do or not to do."
// using toUpperCase method
var upper = sentence.toUpperCase();
console.log(upper);
Example 1:
The following example shows that the original string is untouched while a new string is returned by the method.
Example
var string = "To do or not to do.";
// changing case to upper
var upper = string.toUpperCase();
console.log(upper);
console.log(string);
Example 2:
The following example converts all the lowercase alphabets to uppercase without affecting the integers and special characters.
Example
var string = "The *&*%^ alpha1 @@ 3B.";
// changing case to upper
var upper = string.toUpperCase();
console.log(upper);
JavaScript uppercase first letter
As we know that first letter of a sentence is capitalized. Suppose there is a string with all the small letters, how will you convert the first character to uppercase?
To convert the first letter to uppercase use the toUpperCase method and apply it to the first character using the charAt method. Then use the slice method to cut another part of the string and then concatenate both.
Look at the example below for a better understanding.
Example
var string = "to do or not to do";
// capitalizing the first letter
var capString = string.charAt(0).toUpperCase() + string.slice(1);
console.log(capString);
Uppercase first letter of all words in sentence
To capitalize the first character of all words in a sentence, first, use the split method to split all the words from the sentence.
Now loop through all the words stored in an array and capitalize the first character of the word using the same way used in the above example.
Finally, use the join method to join all the words of the sentence separated by a space.
Example
var string = "to do or not to do";
// capitalizing the first letter of all words
var arr = string.split(" ");
for (let i = 0; i < arr.length; i++) {
arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1)
}
var capString = arr.join(" ");
console.log(capString);
Capitalize first and lower the rest of the characters in the word
The following function converts the first character of the word to uppercase and the rest of the character to lowercase.
Example
function capitalize(word) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
console.log(capitalize("jaVAScripT"));
console.log(capitalize("htML"));
console.log(capitalize("cSs"));
Conclusion
The toUpperCase method can be used to change any string or character to uppercase. Keep in mind that the method is toUpperCase not just upperCase.
You can also use this method to capitalize the first letter of all words in a sentence.