Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array.length vs Array.prototype.length [duplicate]

I found that both the Array Object and Array.prototype have the length property. I am confused on using the Array.length property. How do you use it?

Console.log(Object.getOwnpropertyNames(Array));//As per Internet Explorer

outputs:

length,arguments,caller,prototype,isArray,

Prototype and isArray is usable but how do you use the length property?

like image 965
Maizere Pathak.Nepal Avatar asked Dec 25 '22 23:12

Maizere Pathak.Nepal


2 Answers

Array is a constructor function.

All functions have a length property that returns the number of declared parameters in the function definition.

like image 139
SLaks Avatar answered Dec 27 '22 13:12

SLaks


Array.length is how many arguments the function Array() takes and Array.prototype.length is an instance method that gives you the length of your array. When you check ['foo'].length you're actually checking Array.prototype.length with the this argument being your array ['foo']

var myArray = ['a','b','c']
console.log(myArray.length); //logs 3
like image 38
Bjorn Avatar answered Dec 27 '22 14:12

Bjorn