If length is passed in square bracket notation of an array. It returns first element. What is the underlying logic here?
var foo= [];
foo['0'] = 'hello';
foo[2] = {'fun': true};
foo[length];
If you're running that code in a browser, it's because the window object (which is the global object) has a length property, which is the number of child browsing contexts (iframes and such) that it has. It's 0 if there aren't any (which is often the case). All properties of the window object are globals, so length on its own is the length on window, so foo[length] is foo[0], which is "hello".
That length has nothing to do with your foo array. The length of your foo array is foo.length. If you used it on your last line, you'd get undefined, because at that point foo.length would be 3 and your array doesn't have any entry at foo[3].
foo[length]in global context of browser window is the same asfoo[window.length]. According to documentation,window.lengthreturns "the number of frames (either <frame> or <iframe> elements) in the window."