0

My question is if I use an array like this,

var arr1 = [1, 2, 3, 4];
console.log(arr1.length); // 4 (Expected)

If I update the length property, the array gets updated too.

arr1.length = 0;
console.log(arr1); // []

I'd like to know how this is being done (the internals)?

Also, I'm not sure if getter and setter methods of the property length (that are ways to run a function if the property gets accessed or set) are used here. I think they're not.

If it's being used, length should not be visible directly when I expand the array in the browser console. But we get the length directly without running any functions.

The console when I use get/set for a property of an object,

using get/set

Getting the value of length property directly without running any functions,

length of array

Hari Ram
  • 56
  • 5
  • 1
    Length is a “property”. While custom properties have been able to be defined for a few years now (since ES5), the length property *is intrinsic to the Array type*, with behavior defined by specification, and has existed since the first version. – user2864740 Nov 20 '21 at 07:46
  • 1
    Arrays are a built-in type, and they implement many things specially. `length` acts similar to a getter and setter, but it predates these as general mechanisms. – Barmar Nov 20 '21 at 07:52
  • If you are interested in the specifications, [this](https://262.ecma-international.org/5.1/#sec-15.4.5.1) might be a good resource. – async await Nov 20 '21 at 08:19

0 Answers0