JavaScript String Length Property String.length Property

This tutorial introduces the length property of JavaScript's String object. The length property measures the number of characters in a given string: letters, numbers, spaces, punctuation… The length property is handled "behind the scenes" by your web browser's JavaScript interpreter: length properties are automatically updated as string values are modified.

This tutorial covers the String length property. Are you looking for the Array length property?

Retrieving the Length Property of JavaScript Strings

Length is a read-only property of JavaScript strings: your scripts can retrieve a string's length (the number of characters it contains), but cannot set it to a new length value. This is not necessarily a limitation, since length property is tied with string value: equating a string variable to a four-character string automatically updates the length property to 4.

The length property contains and returns a positive integer, or zero in the case of an empty string (var aString = "";).

var carter = new String("I learn JavaScript.");
alert( carter.length.toString() );

The script above simply declares and initializes a new string variable, then evaluates the string length (the number of characters its value contains). JavaScript String length property

To demonstrate how string length property "auto-updates" itself, let use assign a new value to the string variable we declared above, and retrieve its length.

// Assign a new value to a pre-existing string variable
carter = "I now learn about the String length property.";
alert( carter.length.toString() );

Our string variable's length has been updated: Auto-updated string length property

Measuring the Length Property of Anonymous Strings

Any string value has a length property, not just declared string variables. The script below shows how you can retrieve the length of a quoted string:

alert( "I learn JavaScript.".length.toString() );

Notice the connecting period (.) between the string's closing quotes and the length property. Since the anonymous string has the same value as the declared string we used earlier, the length returned is identical: Length property for anonymous strings

JavaScript can also retrieve the length of strings in HTML text input fields through their value attribute.

String Length Property and Character Position Index

Like JavaScript Array objects, the characters in JavaScript strings are indexed using a zero-based system (where the first element has position zero).

We will soon introduce the charAt() method, a string method that allows you to determine which character is at a given position in a string. This method relies on the often confusing fact that the first character in a string occupies the position zero (and not one: the second character occupies position one).

This means that the last character in a string has a position index equal to the string length minus one (String.length-1).

var carter = new String("abcde");
alert( carter.charAt( carter.length-1 ) );

The script above demonstrates how subtracting one from the length of a string gives you the position index of the last character in that string: String length property and position index

The string variable "abcde" is of length 5, since it contains five characters; its last character has a position index of 4 (String.length-1 = 4). The table below illustrates the position indexes assigned to each character:

Position indexCharacter
0a
1b
2c
3d
4e
Length of string "abcde" is 5

Test the String Length Property

Interactively test the string length property by editing the JavaScript code below and clicking the Test String Length Property button.

Browser support for JavaScript string length property
Internet Explorer supports the JavaScript string length propertyFirefox supports the JavaScript string length propertySafari supports the JavaScript string length propertyOpera supports the JavaScript string length property