Where is array's length property defined?
In JavaScript, the length
property of an array is defined on the Array.prototype
. This means that it is available to all array instances through the prototype chain. The length
property is a read-write property, meaning that you can both read and write its value.
Here is an example of how you can access the length
property of an array:
const arr = [1, 2, 3];
console.log(arr.length); // Output: 3
You can also modify the value of the length
property to change the size of the array. For example:
arr.length = 2;
console.log(arr); // Output: [1, 2]
Note that setting the length
property to a value that is smaller than the current length of the array will remove elements from the end of the array. Setting the length
property to a value that is larger than the current length of the array will add new, undefined elements to the end of the array.