abc
abc

Reputation: 215

How does property length work?

When i loop in a list with several items and use property length does the length property get only the longest word or does it get length of all items?

function onS() {
var maxLength = 0;
var longestListTitle = "";
var Enum = list.getEnumerator();
while (Enum.moveNext()) {
    var currentItem = Enum.get_current();
    var listTitle = currentItem.get_title();
    if (listTitle.length > maxLength) {
        longestListTitle = listTitle;
        maxLength = longestListTitle.length;
    }
}

Upvotes: 0

Views: 41

Answers (2)

Magus
Magus

Reputation: 15124

length works differently on String or Array.

On a string, length is the total length of the string with all caracters. "test test".length will give you 9.

On a array, it can be more tricky. [].length is obvioulsy 0. ['test'].length is 1. But you can do this :

var test = [];
test[0] = 1;
test[10] = 2;
console.log(test.length); // output 11

For an array, length is the maximum index used + 1.

Upvotes: 2

Asad Palekar
Asad Palekar

Reputation: 301

If its an array it will give you the length of the array (total elements in array). If its a string, it gives you the length of the string (total characters in the string.)

Upvotes: 1

Related Questions