Capitalize the first letter of a string in Typescript is a task which will be very helpful when you want to handle the user name. So how to do it? Let’s go into detail now.
Capitalize the first letter of a string in Typescript
Use the trim method
The trim()
method will remove all white spaces of the string from the start and the end of the string.
Syntax:
string.trim()
Example:
const aString = ' Hello From Learn Share IT
// Remove all while space at first and end of the string
const trimedString = aString.trim()
console.log(trimedString)
Output:
[LOG]: "Hello From Learn Share IT"
Use the charAt() method
The charAt()
method will help you return the letter at the specified index.
Syntax:
string.charAt(index)
Parameters:
- index: Indicate the position in which you want to get the letter.
Example:
const aString = 'Hello From Learn Share IT'
// Return the letter at index 3
const aLetter = aString.charAt(3)
console.log(aLetter)
Output:
[LOG]: "l"
Use the toUpperCase() method
The toUpperCase()
method will help you to capitalize all letters in the string.
Syntax:
string.toUpperCase()
Example:
const aString = 'Hello From Learn Share IT'
// Capitalize all letter in aString
const capitalizeStr = aString.toUpperCase()
console.log(capitalizeStr)
Output:
[LOG]: "HELLO FROM LEARN SHARE IT"
Use the slice() method
The slice method will help you return all characters you selected in the string.
Syntax:
string.slice(start, end)
Parameters:
- start: The position that you want to start select the letter.
- end: The position that you want to end select the letter.
Example:
const aString = 'Hello From Learn Share IT'
// Slice all letter from index 11 to index 25
const selectedLetter = aString.slice(11, 25)
console.log(selectedLetter)
Output:
[LOG]: "Learn Share IT"
If I don’t pass in the end parameters, it will default set to the length of the string.
Example:
const aString = 'Hello From Learn Share IT'
// Slice all letter from index 6 to the end
const selectedString = aString.slice(6)
console.log(selectedString)
Output:
[LOG]: "From Learn Share IT"
Capitalize the first letter of the string
The logic here is I will use the trim method to remove all white spaces and use charAt() method to get the letter at the first letter, then use the upperCase method to capitalize that letter, then use the slice method to concatenate with the last part of the string.
Example:
const aString = ' James '
// Remove while space
const trimedString = aString.trim()
// Get the first letter, capitalize it then concatenate with the left string
const changedString = trimedString.charAt(0).toUpperCase() + trimedString.slice(1)
console.log(changedString)
Output:
[LOG]: "James"
Summary
In this article, I showed you how to capitalize the first letter of a string in Typescript. You can combine all methods trim, charAt, toUpperCase, and slice methods to do it. I hope it helpful to you. Let’s try these methods!
Maybe you are interested:
- Define an Array of Strings in TypeScript
- Replace all occurrences of a String in TypeScript
- Convert a String to a Boolean in TypeScript

Hello, guys! I hope that my knowledge in HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, Python, MySQL, and npm computer languages may be of use to you. I’m Brent Johnson, a software developer.
Name of the university: HOU
Major: IT
Programming Languages: HTML, CSS, JavaScript, TypeScript, NodeJS, ReactJS, MongoDB, PyThon, MySQL, npm