capitalize a string in typescript

To capitalize a string in TypeScript, you can use either toUpperCase() or toLocaleUpperCase() method of the string object.

index.ts
let str: string = "hello world";
let capitalizedStr = str.toUpperCase();
console.log(capitalizedStr);
102 chars
4 lines

Output: "HELLO WORLD"

If you want to capitalize only the first letter of the string, you can use the slice() method as shown below:

index.ts
let str: string = "hello world";
let capitalizedStr = str.charAt(0).toLocaleUpperCase() + str.slice(1);
console.log(capitalizedStr);
133 chars
4 lines

Output: "Hello world"

gistlibby LogSnag