In this typescript tutorial, we will see how to capitalize the first letter of a string in typescript.
There are four methods in typescript to capitalize the first letter of the string:
- Using
charAt
andtoUpperCase
- Using the spread operator and
toUpperCase
- Using a regular expression and
replace
- Using
slice
,toUpperCase
, and string concatenation
Using charAt
and toUpperCase
Here we will see how we can capitalize the first letter of a string in typescript using the charAt and toUpperCase methods.
The charAt(0)
method returns the first character of the string and toUpperCase()
is used to capitalize it in typescript.
For example, we will take a string, and we will get the first character/letter using charAt(), then we will capitalize it using toUpperCase
(). At last, we will merge the capitalized letter with rest of the string using slice() in typescript.
Open the code editor, then create a file named as ‘CapitalizeFirstLetter.ts’, and write the below code.
function capitalizeFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1);
}
// Example:
const myString = "canada";
const capitalizedString = capitalizeFirstLetter(myString);
console.log(capitalizedString); //👉 "Canada"
To compile the code run the below command, and you can see the result in the console.
ts-node CapitalizeFirstLetter

This is an example of capitalizing the first letter of a string in typescript using the charAt and toUpperCase methods.
Using the spread operator and toUpperCase
Here we will see how we can capitalize the first letter of a string in typescript using the spread operator and toUpperCase methods.
For example, we will use the spread operator ([...str]
) to convert the string to an array of letters, then get the first element of the array and capitalize it using toUpperCase
. At last, we concatenate the capitalized letter with the rest of the string using slice
() method.
In the CapitalizeFirstLetter.ts file write the below code:
function capitalizeFirstLetter(str: string): string {
return [...str][0].toUpperCase() + str.slice(1);
}
// Example:
const myString = "canada";
const capitalizedString = capitalizeFirstLetter(myString);
console.log(capitalizedString); //👉 "Canada"
To compile the code run the below command, and you can see the result in the console
ts-node CapitalizeFirstLetter

This is an example of capitalizing the first letter of a string in typescript using the spread operator and toUpperCase methods.
Using a regular expression and replace
Here we will see how we can capitalize the first letter of a string in typescript using the regular expression and replace methods.
For example, we will use a regular expression to match the first-word letter in the string, then pass it to the toUpperCase
method to capitalize it.
In the CapitalizeFirstLetter.ts file write the below code
function capitalizeFirstLetter(str: string): string {
return str.replace(/^\w/, (c) => c.toUpperCase());
}
// Example:
const myString = "canada";
const capitalizedString = capitalizeFirstLetter(myString);
console.log(capitalizedString); //👉 "Canada"
To compile the code run the below command, and you can see the result in the console.
ts-node CapitalizeFirstLetter

This is an example of capitalizing the first letter of a string in typescript using the regular expression and replace methods.
Using slice
, toUpperCase
, and string concatenation
Here we will see how we can capitalize the first letter of a string in typescript using the slice toUpperCase and string concatenation method.
For example, we will use the slice
method to get the first letter of the string, then toUpperCase
() to capitalize it. At last, we concatenate the capitalized letter with the rest of the string using slice
().
In the CapitalizeFirstLetter.ts file write the below code
function capitalizeFirstLetter(str: string): string {
return str.slice(0, 1).toUpperCase() + str.slice(1);
}
// Example:
const myString = "canada";
const capitalizedString = capitalizeFirstLetter(myString);
console.log(capitalizedString); //👉 "Canada"
To compile the code run the below command, and you can see the result in the console
ts-node CapitalizeFirstLetter

This is an example of capitalizing the first letter of a string in typescript using the slice toUpperCase and string concatenation method.
Conclusion
In this typescript tutorial, we saw how to capitalize the first letter of a string in typescript using different methods in typescript. So the different typescript methods we covered are listed below.
- Using
charAt
andtoUpperCase
- Using the spread operator and
toUpperCase
- Using a regular expression and
replace
- Using
slice
,toUpperCase
, and string concatenation
You may like the following typescript tutorials:
- Typescript string union vs enum
- Typescript split string by dot
- How to split a string with multiple separators in TypeScript?
- How to compare two strings in typescript
I am Bijay a Microsoft MVP (8 times –Â My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com