4

as i know one can return the length of an array either by using

array.length

or

array["length"]

but omitting the quotes like this

array[length]

will return the first element of the array This is the actual code: var list = [5, 7, 10]; then in the console list[length] returns 5, and no i don't have iframe or frames in the window, so window.length or just length return 0 in the consol.

  • 4
    Congrats, you found the global `length` property of the `window` object. – Shadow The GPT Wizard Sep 11 '16 at 12:26
  • It is easy because windows have a property of length and it depends on the length of the window (let's call it browser tab) you are in, it will return different number, it is number of frames (maybe iframes) of a page, in your case, it returns 0 – Mohammad Kermani Sep 11 '16 at 12:29

2 Answers2

3

You are using window.length.

Returns the number of frames (either <frame> or <iframe> elements) in the window.

var array = [21, 22];

console.log(length);
console.log(window.length);
console.log(array[length]);
<iframe></iframe>
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • it is the same scenario of the last statement console.log(array[length]); , but you are saying that i am using window.length which i am not –  Sep 11 '16 at 12:31
  • here is my code: var list = [5, 7, 10]; then i type this in the console : list[length]; which returns 5 –  Sep 11 '16 at 12:31
  • @Hassan, please watch the difference now with an iframe. both values of `length` are now one. – Nina Scholz Sep 11 '16 at 12:36
  • 1
    I get it now, Thanks Nina :) –  Sep 11 '16 at 13:12
1

Length equates to numerical 0 which returns the first element if it exists.

Numerical 0 since you have a browsing context (Document) and no other contexts.

Be cognitive of other environments such as nodejs, NW.js etc. which may exist and may differ from the browser context.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100