Array.length vs Array.prototype.length

时间:2023-03-08 21:37:05
Array.length vs Array.prototype.length

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?

Answer:

Array is a constructor function.

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

Array.length vs Array.prototype.length

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