NOTE: As of writing this answer, the linked question on main has been reopened.
Not a new question
I support the decision by Kevin B to close the question. The simple reason is that there is no shortage of explanations of the behaviour. There were two duplicates. In order:
Why can I add named properties to an array as if it were an object?
Add property to javascript array
The first one itself has 42 linked questions.
Many times you'd find the same information repeated when PHP programmers ask about "associative arrays in JavaScript". There are associative array in PHP and because the terminology being a bit confusing:
- "array" shows up in both both names
- arrays in JavaScript could work similar to associative arrays but not quite
So many questions from people with PHP background use the term "associative array" and in many cases that requires explaining why you can add properties to arrays and why that's not a good idea
Google lists 15 thousand hits for "associative array javascript" when restricted to only search in Stack Overflow.
Stack Overflow search lists almost 8 thousands results for a similar query restricted to javascript. When further restricting the search to questions only we get 3.5 thousand results.
Not all of those are directly related but many are similar. Examples:
Javascript foreach loop on associative array object
JavaScript associative array to JSON
Iterate over a Javascript associative array in sorted order
(screenshot of the where I got these - three questions the top 10 results when sorted by relevance)
All of this is to say that there is no shortage of questions about this behaviour. Not sure why we'd need yet another one.
No new answers
Here are quotes from answers to your question with timestamps of the most current revision (as of writing this):
An array in JavaScript is not a separate type, but a subclass of object
Mark Reed, 2021-04-18
The reason why it works is that it is still an object.
MertDalbudak, 2021-04-14
In JavaScript, an array is an object.
Nguyễn Văn Phong, 2021-04-10
An array is a special object that uses integers as its keys
Vo Quoc Thang, 2021-04-21
Arrays are so-called "Integer Indexed Exotic Objects" in ECMAScript which are based on which in return belong to the object data type.
shaedrich, 2021-04-21
This is because array is itself an object with specific behaviour.
Abhishek Pankar, 2021-04-14
EPIPHANY moment — arrays ✨ ARE ✨ objects.
Brandon McConnell, 2021-04-17
You can see a theme - every single answer points out that arrays are objects. And that's really the answer to your question. Here is what the accepted answer in each of the two duplicates also says:
Virtually everything in javascript is an object
Paul Dixon, 2009-05-17
Arrays are already objects in JavaScript
Kevin Ennis, 2012-05-31
So, every single answer had to point out information that was available for a decade. Sure, each answer has expanded in different ways on this but the core of the why in your question is in each quote above. Arrays are objects.
The new question is distinct due to scope
Permutations, permutations
You said:
I have 4 concerns. They are totally different from the old question.
And here is the relevant excerpt from the main question:
Question:
- Why have this behavior? This seems inconsistent, right?
- What is the data structure actually storing behind the scene: as an array or something like an object, hash table, linked list?
- Whether this behavior depends on a specific JavaScript Engine (V8, SpiderMonkey, etc..) or not?
- Should I use arrays like this (keys are both numeric index and string) over normal objects?
First of all, that's four questions. We normally want one question per question. Otherwise, you can have (effectively) infinite amount of questions that are "unique" because they are just permutations of other questions. If we have questions A, B, and C, you can ask for
- A + B
- A + C
- B + C
- A + B + C
Then say that each is not a duplicate because A doesn't answer it, nor does B, nor does C.
In the case with Stack Overflow, we more realistically have a thousand variations of A, maybe several hundred for B, then another couple of thousand for C. There are not enough duplicate slots to link everything together. None of this is really distinct information, either.
Even then, still no new information
Here is the first paragraph of the accepted answer to the top duplicate:
Virtually everything in javascript is an object, so you can "abuse" an Array object by setting arbitrary properties on it. This should be considered harmful though. Arrays are for numerically indexed data - for non-numeric keys, use an Object.
It actually answers three of your questions
- #1 - It is a standard behaviour, because arrays are objects.
- #3 - It is standard behaviour - that's how objects behave. So, it's not related to any specific engine.
- #4 - there is a link to a blog from 2006 that discusses why using custom properties on arrays is undesirable.
And here is a question that is linked in the second duplicate:
What are the drawbacks of setting string properties on arrays?
It's from 2014.
OK, so what about #2?
Two things:
- This is implementation-dependent question. Each engine is free to handle things "behind the scene" as they like. It's not possible to give a precise answer, because this can vary between engines and between different versions of the same engine. How Chrome handled things a decade ago has changed. Another decade later, Chrome might have a different implementation of arrays. Same with Firefox, same with any other environment.
- With all that said, it's not like people haven't tried to ask and answer this:
And here are links to the first ten results:
How are javascript arrays implemented?
How is an "array" implemented in JavaScript?
Are javascript Arrays actually implemented as arrays?
How are arrays implemented in javascript? What happened to the good old lists?
What data structure are arrays implemented with in JavaScript?
How are JavaScript arrays stored in memory
How are JavaScript arrays represented in physical memory?
Implement Array-like behavior in JavaScript without using Array
Is an array in JS is a pointers array?
How are the JS Arrays internally resizing?
Some are more relevant, some are less. Some of the information is also outdated. However, the point here is that question number 2. alone is a big one. Or a really tiny one answered by a single sentence "It's implementation-dependent." but a separate question, nonetheless.
Questions, questions, questions
There are many, many questions on this topic. Yes, some of the specific information might have been covered in more than one existing questions. But to make a new one because no single question covers it? Seems similar to this:
Only we started with literally thousands here.
We want Stack Overflow to be a place where you go to find specific answers to specific questions. There is an argument to be made that we might need a cleanup around this topic. However, fracturing the knowledge even further does not help.
console.log
output and that is not standard! Consoles are (in)famous in having freedom in outputting data, and specifically objects, as they deem fit. Relying on a single uniform representation is, and has always been, wrong.