Length should return the value of the last index + 1, so in that case length will return 8: http://jsfiddle.net/
<http://jsfiddle.net/>.length is defined that way so you can safely use it to iterate over all the elements of an array with a for loop. AFAIK, `arr[arr.length] = x;` is functionally equivalent to `arr.push(x);`, but it doesn't surprise me that there'd be performance differences, which will depend on how aggressively the engine tries to optimise. On 16 March 2011 10:43, Rob Griffiths <[email protected]> wrote: > >> Why it is better to use a function instead of direct access? >> >> > It's not always better. > > Push is far better when you're not sure what the next index of your array > is. for example: > > [87, 2, 369, , , , 42, 53] > > If i remember correctly .length will return 5, which means if you started > adding in items from that point, you'll eventually overwrite other items in > the array. > > Direct access is useful when you are creating the array from scratch, for > instance holding the properties of a object. > > -- > Rob Griffiths > http://bytespider.eu > @bytespider <http://twitter.com/bytespider> > > https://github.com/bytespider > > -- > To view archived discussions from the original JSMentors Mailman list: > http://www.mail-archive.com/[email protected]/ > > To search via a non-Google archive, visit here: > http://www.mail-archive.com/[email protected]/ > > To unsubscribe from this group, send email to > [email protected] > -- Nick Morgan http://skilldrick.co.uk @skilldrick <http://twitter.com/skilldrick> -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
