Your first example outputs 0 because when you do $(cars)
, you're wrapping your cars
object in a jQuery object, an object with one element with index 0.
Also you should note the distinction between both "each" methods:
jQuery.each() or $.each():
A generic iterator function, which can be used to seamlessly iterate over both objects and arrays.
Iterate over a jQuery object, executing a function for each matched element.