length Property (String) (JavaScript)

Returns the length of a String object.

Caution note Caution

JavaScript strings are immutable, so the length of a string cannot be modified.

strVariable.length
"String Literal".length 

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 ilength - 1.

The following code shows how to use length. JavaScript strings are immutable and cannot be modified in place. However, you can write the reversed string to an array and then call join with the empty character, which produces a string with no separator characters.

var str = "every good boy does fine";
        var start = 0;
        var end = str.length - 1;
        var tmp = "";
        var arr = new Array(end);

        while (end >= 0) {
            arr[start++] = str.charAt(end--);
        }

// Join the elements of the array with a 
        var str2 = arr.join('');
        document.write(str2);

// Output: enif seod yob doog yreve

Supported in the following document modes: Quirks, Internet Explorer 6 standards, Internet Explorer 7 standards, Internet Explorer 8 standards, Internet Explorer 9 standards, Internet Explorer 10 standards, Internet Explorer 11 standards. Also supported in Store apps (Windows 8 and Windows Phone 8.1). See Version Information.

Show:
© 2014 Microsoft