0

What's the purpose of length?( I think its used for counting)

in the example <p>hello</p> <p>good day</p> <p>hi</p> <script> var=arr document.getElementTagName("p"); for(var x=0; x<arr.length;x++); {arr [x]=get.InnerHTML="hi there";} </script> //which results in all the "p" changing to hi there. what difference does "length" add to the code? and what's its purpose?

30th Jan 2017, 3:54 PM
Codie Richards
Codie Richards - avatar
2 odpowiedzi
+ 2
'length' is a property of arrays, not a method: it store the count of elements, but don't count them every time it's accessed ;) Strings, are almost like arrays of char, so mostly properties and method of arrays exist too for strings...
30th Jan 2017, 5:41 PM
visph
visph - avatar
+ 1
length in this case will count the items in your array, in your array we have 3 elements, so your for loop will go from 0 to 2 (x<arr.length). You can use length to count the string characters too, for example: var a = "test"; var b = a.length; in the var b we will have the number 4! PS: sorry for my bad english, i'm brazilian!
30th Jan 2017, 4:03 PM
N1NJ4BR