The documentation for the JavaScript language's built-in global Object object states that
Object.length
Has a value of 1.
What is the significance of this property and, additionally, why isn't object.length available to us in the same manner as array.length?
At first glance, it would seem to me that the reason for my latter question has to do with a possible confusion in the interpreter of the length property on the object with the dot notation method of property access/initialization.
The
lengthproperty specifies the number of arguments expected by thefunction.
As Object belongs to type function and length property of function
determines the number of arguments, Object.length returns the length of arguments being expected.
Consider following example:
function test(a, b, c) {}
console.log(test.length);
And with Object:
console.log(typeof Object);
console.log(Object.length); //returns 1
"why isn't object.length available to us in the same manner as array.length?"
Note that array is also of type object(Try typeof []) hence "why isn't object.length available to us" is not literally correct. If you are referring to object-without-indices, we read the object property using the key of the object, if there is no key as length, obj.length returns undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With