Tutorials Tonight
Follow Us

JavaScipt upperCase - Convert Case Of String To Uppercase


In this tutorial, you will learn how to convert any case of a string to uppercase and will also look at ways to capitalise the first character of each word in a sentence.

JavaScipt toUpperCase Method

The toUpperCase method is a javascript 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 return a new string after capitalisation.

Example

let sentence = "To do or not to do."
console.log(sentence.toUpperCase());
▶ Try It

Example 1:

The following example shows that the original string is untouched while a new string is returned by the method.

Example

let string = "To do or not to do.";
let capString = string.toUpperCase();
console.log(capString);
console.log(string);
▶ Try It

Example 2:

The following example converts all the lowercase alphabets to uppercase without affecting the integers and special characters.

Example

let string = "The *&*%^ alpha1 @@ 3B.";
let capString = string.toUpperCase();
console.log(capString);
▶ Try It

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

let string = "to do or not to do";
// capitalizing the first letter
let capString = string.charAt(0).toUpperCase() + string.slice(1);
console.log(capString);
▶ Try It

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 word 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 word of the sentence separated by a space.

Example

let string = "to do or not to do";
// capitalizing the first letter of all words
let arr = string.split(" ");
for (let i = 0; i < arr.length; i++) {
  arr[i] = arr[i].charAt(0).toUpperCase() + arr[i].slice(1)
}
let capString = arr.join(" ");
console.log(capString);
▶ Try It

Capitalize first and lower the rest of the characters in 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"));
▶ Try It

Points to remember:

  1. To capitalize the string in javascript using the toUpperCase() method on the string.
Follow Us

Copyright © Tutorials Tonight. All Rights Are Reserved.