Learn to use Javascript string length property

The string length property of Javascript

The string length property is used to return the length or number of characters in a given string. The JS length property returns int number.

The string length property may be useful is different scenarios like allowing the visitors a limited number of characters in a textarea etc.

See an example of length property

Syntax of using string length method

Following is the string length, javascript property syntax:

Str.length;

Where Str is a string variable. The length property will return a total number of characters of Str variable.

A length property example

Following is a javascript length property example. We have created a string object and then used length JS property. The alert will show total characters or length of string. See example online by clicking the link:

Experience this example online


As you click on the button “Show string length” the alert will show the length of string by using the length property.

String length javascript example with textarea

In this example, we are using a textarea to show the use of length property. Enter some text in textarea and then press the button. The alert will show total characters entered, by using Javascript length property.

Experience this example online


Javascript length example with string concatenation

Following example uses length property, after concatenating two strings. The length method returns total characters after concatenation, as shown below:

Experience this example online


AS you can see two strings are concatenated and an alert shows total characters after using javascript string length property.

Related Javascript String | Javascript substring | Javascript replace | Javascript Array Length

Was this article helpful?

Related Articles

Leave A Comment?