How do arrays behave in JavaScript?
An array in JavaScript is really an object that has a special length property and a set of keys that look like numbers. The language gives arrays their own methods, but the storage underneath is the same as any other object. That is why you can add named properties to an array, and it is also why typeof reports an array as object.
const fruits = ['apple', 'banana'];
fruits.push('cherry');
console.log(fruits.length); // 3
console.log(fruits[0]); // apple
console.log(fruits[fruits.length - 1]); // cherry
fruits[1] = 'blueberry';
console.log(fruits); // apple, blueberry, cherry
// An array is an object, so it can hold extra named properties.
fruits.count = 3;
console.log(fruits.count); // 3
console.log(Array.isArray(fruits)); // true
The length property is looked after for you. When you push an item onto the end, the length grows by one. When you write to the index just after the end, the array grows to include it. And when you set the length to a smaller number, the elements past that point are deleted.
The items are kept in order, but the engine does not promise that they sit next to each other in memory. Modern engines do keep a full array in one block so that reading an item is fast, and they switch to a slower dictionary style when the array has gaps in it. That is one reason that creating an array with a fixed length and filling it one index at a time is slower than pushing onto an empty array.
Two practical notes. Use Array.isArray() to test for an array instead of comparing the type, because the type test gives you the generic object answer. And treat an array as a shape that you own. Adding named properties to it is legal, but it breaks the assumptions of any code that loops over the array or turns it into JSON, and JSON output simply ignores those extra properties.