The Array Problem

The JavaScript array object behaves in ways that developers couldn’t mimic in their own objects before ECMASCript 6. An array’s length property is affected when you assign values to specific array items, and you can modify array items by modifying the length property. For example:

  1. let colors = ["red", "green", "blue"];
  2. console.log(colors.length); // 3
  3. colors[3] = "black";
  4. console.log(colors.length); // 4
  5. console.log(colors[3]); // "black"
  6. colors.length = 2;
  7. console.log(colors.length); // 2
  8. console.log(colors[3]); // undefined
  9. console.log(colors[2]); // undefined
  10. console.log(colors[1]); // "green"

The colors array starts with three items. Assigning "black" to colors[3] automatically increments the length property to 4. Setting the length property to 2 removes the last two items in the array, leaving only the first two items. Nothing in ECMAScript 5 allows developers to achieve this behavior, but proxies change that.

I> This nonstandard behavior is why arrays are considered exotic objects in ECMAScript 6.