To get the length of a String in TypeScript, you can use length property of the String object.

When you use the length property on the String object, it returns an integer that represents the length of the string.

String Length in TypeScript

In the following example, we will take a string variable, initialize it with some value and then find its length.


var str = new String("Hello World") 
var len = str.length
console.log("The length of above string is : "+len)

When you convert the code into JavaScript using the command tsc example.ts, you will get the following JavaScript Code.


var str = new String("Hello World");
var len = str.length;
console.log("The length of above string is : " + len);

If you run this JavaScript code in a web-browser, the output in the console would be as shown in the following screenshot:

TypeScript String Length

You can also use the length property directly on a string constant.


var len = "Hello World".length 
console.log("The length of above string is : "+len)

The output would be same as in the above example.

TypeScript String Length