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?
2 Answers
+ 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...
+ 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!