How to uppercase or lowercase all characters of a string in TypeScript

Introduction :

Converting all characters to uppercase or lowercase in typescript is similar to javascript. Typescript provides two simple methods to convert all characters to uppercase and to lowercase. In this tutorial, we will learn these two methods with examples.

Convert all characters to uppercase :

To convert all characters to uppercase, toUpperCase() method is used. The syntax of this method is as below :

str.toUpperCase()

It converts all characters of str to uppercase and returns the result string. Note that the original string will remain the same. For example :

let givenStr = "Hello World !!";
let upperCaseStr = givenStr.toUpperCase();

console.log(`Given string : ${givenStr}`);
console.log(`Modified string : ${upperCaseStr}`);

It will print the below output :

Given string : Hello World !!
Modified string : HELLO WORLD !!

We are printing the strings at the end of the program i.e. after toUpperCase() is called. As you can see here, String givenStr is not modified. toUpperCase creates one new string and returns that string.

Convert all characters to lowercase :

Similar to the above, we can also convert all characters to lowercase using toLowerCase() method. Similar to toUpperCase(), it also returns one new string without changing the original string. Let’s try the above example toLowerCase()

let givenStr = "Hello World !!";
let lowerCaseStr = givenStr.toLowerCase();

console.log(`Given string : ${givenStr}`);
console.log(`Modified string : ${lowerCaseStr}`);

It will print the below output :

Given string : Hello World !!
Modified string : hello world !!

Example with both toUpperCase and toLowerCase :

let givenStr = "Hello World !!";
let lowerCaseStr = givenStr.toLowerCase();
let upperCaseStr = givenStr.toUpperCase();

console.log(`Given string : ${givenStr}`);
console.log(`Lower case string : ${lowerCaseStr}`);
console.log(`Upper case string : ${upperCaseStr}`);

Output :

Given string : Hello World !!
Lower case string : hello world !!
Upper case string : HELLO WORLD !!

TypeScript uppercase lowercase

Where is the color and why codevscolor ?

Long story short, I love paintings and I paint on weekends. We(me and my wife) have one Youtube channel. Below is a video that I did recently. If you love this please do subscribe to support us 😊❤️❤️