JavaScript tutorial:
length property (String)

 

Applies to: String Object

The length property returns the length of a String object.

Syntax

strVariable.length
"String Literal".length

Return value

The length property returns the length of the String object

Example

The length property contains an integer that indicates the number of characters in the String object. The last character in the String object has an index of length - 1.

function compareLength(str1, str2)
{
if(str1.length <str2)
return -1;
else if(str1.length==str2)
return 0;
else
return 1;
}
document.write(compareLength("i am superman", " i am batman"));

To run the code, paste it into JavaScript Editor, and click the Execute button.

See also: length property (Array), length property (Function), String Object methods, String Object properties