0

When I try to create a variable named length

var length = 10;

JSLint complaints

Redefinition of 'length'. var length = 10;

But where does that length come from?

Fiddle

helpermethod
  • 59,493
  • 71
  • 188
  • 276
  • Could you post the js you're using? I'm assuming you're either overriding a global, or you have another length variable declared somewhere within your function (or it's parent function). – JesseBuesking Dec 02 '11 at 22:10

2 Answers2

5

window already has length defined:

window.length : Window Object

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 1
    To further explain, if you are at the global scope (way up top, not in a function) and you create a variable with the same name as a property on window, then your variable *is* that property on window. A bizarre gotcha in JavaScript. – Matt Greer Dec 02 '11 at 22:14
  • +1 Very obscure that length returns the number of frames within the window. JS can give some real headaches :-). – helpermethod Dec 02 '11 at 22:22
  • 1
    JS is like an American muscle car. A ripe pain in the ass, but oh so worth it :) – Matt Greer Dec 02 '11 at 22:28
0

If you had, say, an array:

howlong=myarray.length;

would return the number of elements in the array.

If you had a string:

howlong=mystring.length;

would return the number of characters in the string.

Long story short, 'length' is used all over the place. Most useful.

Terry
  • 1,437
  • 2
  • 12
  • 26