TiTi
TiTi

Reputation: 363

Why does jQuery.length == 2?

For both current stable jQuery versions (1.11.0 & 2.1.0), I've noticed that jQuery.length == 2 after jQuery is loaded.

Why is that ?

Both jQuery[0] and jQuery[1] are undefined.

I'm familliar with the jQuery array-like objects and console behavior regarding arrays/objects and objects with .length and .splice().

Just wondering about jQuery.length

Upvotes: 6

Views: 377

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149020

jQuery is a function, and the length property of a function equals the number of formal (declared) parameters to the function. From MDN:

length is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number does not include the rest parameter. By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.

Specifically, the jQuery function has this signature (source):

jQuery = function( selector, context ) {
    ...
}

Upvotes: 11

SLaks
SLaks

Reputation: 887469

jQuery is a function.
The length property of a function returns the number of declared parameters.

Upvotes: 10

Related Questions