Using .length to check for last slide/dot?


In "Interactive Website: Flipboard", steps 5 and 7, I can follow the instructions and make it work but I don't understand why we use .length to check if we've reached the last slide or dot.

Isn't .length used for determining the length of a string?


Actually, both strings and arrays have a length property. (Objects do too, but it is always 0.)

The length will be zero (0) if there is no element node present at that index in the collection. That's how we can tell we've reached the end, or at the beginning.


I think I figured it out after I posted the question. I found it in the jQuery API documentation. If I was working with a string, then .length would be just a number representing the number of characters, but in this case it's a jQuery object. For example $('.active-slide').next() would match the next sibling of the active slide and .length on a jQuery object returns the number of elements matched. If it's the last slide, that matches 0 elements.

I forgot that it's not just a general JavaScript function, it matters what type of thing comes before the dot.