1

I have this question Why does array allow string as an index in JavaScript.

It was flagged as a duplicate of Add property to javascript array by KevinB.

The problem is that I actually come from C# (strongly typed) language so I really was surprised by the strange syntax. After I googled I didn't find the answer. I have 4 concerns. They are completely different from the old question.

I requested them to review again but they added another old question as a dupe target. Unfortunately, this also did not cover my concerns.

I already requested them to vote to re-open this question and get the 2 other people's opinions instead of using the "gold badge holder" privilege, but they didn't.

Furthermore, that comment has been removed right after, and I got some downvotes later although I got 8 upvotes before pinging.


PS: I need to get other advice. Kindly let me know what should I do. I don't want to argue. I'm willing to close this kind of question at meta if needed.

7
  • 1
    Here are the first two sentences of the accepted answer: "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" it addresses 1, 3, and 4 of your list of questions.
    – VLAZ
    Commented Apr 21, 2021 at 5:09
  • The above was supposed to say "the accepted answer of the top duplicate".
    – VLAZ
    Commented Apr 21, 2021 at 5:20
  • 1
    I'm not sure it addresses #3 of my list of questions @VLAZ. Besides, the answers to my question having more detailed explanations including examples. So IMO, it's quite different from the old one. Commented Apr 21, 2021 at 6:27
  • You asked whether it's dependent on the engine. The answer states an array is an object and that's how all objects behave. It doesn't explicitly state that all engines handle object interactions the same way but I'm not sure it needs to - seems implicit that standard behaviour would be the same across the board.
    – VLAZ
    Commented Apr 21, 2021 at 6:41
  • How about @MertDalbudak's answer? It seems to me that the behavior (Array alows string as key) depends on the implementation/browser? Commented Apr 21, 2021 at 6:47
  • 2
    Very simple: the answer is wrong. It relies on the 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.
    – VLAZ
    Commented Apr 21, 2021 at 6:51
  • Yes. I agree with you. @VLAZ I assuming that the different implementation with each engine but didn't find the reliable docs. so maybe it like you said Commented Apr 21, 2021 at 6:52

3 Answers 3

8

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. Google search screenshot listing 15800 results for the term "associative array javascript site:stackoverflow.com"

Stack Overflow search lists almost 8 thousands results for a similar query restricted to . 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:

  1. Why have this behavior? This seems inconsistent, right?
  2. What is the data structure actually storing behind the scene: as an array or something like an object, hash table, linked list?
  3. Whether this behavior depends on a specific JavaScript Engine (V8, SpiderMonkey, etc..) or not?
  4. 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:

  1. 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.
  2. With all that said, it's not like people haven't tried to ask and answer this:

Google search screenshot listing 43200 results for the term "how are arrays implemented javascript site:stackoverflow.com"

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:

XKCD comic #927 title: Standards

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.

5
  • Yes. I agree with you. I'm voting to close my question by myself. Thanks for your useful research. Hopefully, I won't get additional downvote. I'm happy with your support. Commented Apr 21, 2021 at 16:19
  • That's beautiful :) My reasoning for supporting reopen was that I would really like to see both Q&As be closed against something better, though. In the target, the accepted answer does not provide any reference as to why things the way they are. The second is the same, but dives into discussing Douglas Crockford's "The Good Parts" which is nice, but not very informative either. The third is supplementary and only deals with defining non-enumerable custom props. The fourth one is a list of examples. And the last one is no better - boils down to "yes". At least the answers in the new one [1/2]
    – 0Valt
    Commented Apr 21, 2021 at 16:24
  • Yes. That's exactly what I was looking for. Happy coding ^^! Commented Apr 21, 2021 at 16:27
  • 1
    [2/2] tried to dive deeper into the subject matter. Plus now there is an in-depth discussion in comments (for which, btw, a separate sincere thank you). The Q&A certainly benefited from the curation it got after it was brought up on Meta, and I am not against reclosing it (since the OP confirmed they want to do that), just wish we could settle on a more thorough Q&A pair.
    – 0Valt
    Commented Apr 21, 2021 at 16:28
  • To be honest, I get the useful answer from @VLAZ 's summary, research over the other ones. Commented Apr 22, 2021 at 12:34
4

I'm of the opinion that this particular quirk of JavaScript is simply one that needs no further discussion than the duplicate provides. It's a "feature" that should never be used, particularly by anyone who doesn't already know the answer.

Both questions do ask the same thing and cover the same topic, and I felt that the older one was far more efficient at getting to the point above.

I'd rather leave it to anyone else who wishes to vote on it to vote as they please than to reverse my own vote

12
  • " this particular quirk of javascript is simply one that needs no further discussion than the duplicate provides. It's a "feature" that should never be used" You mean we should ignore it instead? Commented Apr 21, 2021 at 2:44
  • 1
    I mean, any answer pointing it out should be clear, concise, and to the point. it doesn't need 3 pages of discussion
    – Kevin B
    Commented Apr 21, 2021 at 2:45
  • Actually, there are numerous Q&A like that. They are dived deeper inside instead. The concerns totally related to the main question, They just help clarify then have a bigger picture :) Commented Apr 21, 2021 at 2:48
  • 4
    it... doesn't though. there's nothing to delve into here, arrays are objects, and that means exactly what it does
    – Kevin B
    Commented Apr 21, 2021 at 2:48
  • You could certainly argue that it may be worth mentioniing prototypical inheritance... but... i feel like that's an entirely separate subject that literal books have been written about, that's far broader than why can arrays have string keys (and we probably have a canonical for that)
    – Kevin B
    Commented Apr 21, 2021 at 2:51
  • How about the concern #2, #3. Obviously, the old question didn't point out them. Why don't we have more discussion? Like arrays topic, There millions of question-related to it. And it just your opinion. Someone will respect that :) Commented Apr 21, 2021 at 2:52
  • #2: arrays are objects, and #3 no, do you have a reason to believe there could be? what kind of question is that?
    – Kevin B
    Commented Apr 21, 2021 at 2:54
  • #2 yes, but how the engine has a special treatment with key as integer index. #3 Obviously, the implementation of JavaScript depends on a specific engine/browser. Commented Apr 21, 2021 at 2:57
  • your #2 expansion isn't at all related to string properties on arrays.
    – Kevin B
    Commented Apr 21, 2021 at 2:58
  • and yes, If I know all about it, I don't need to ask :) Commented Apr 21, 2021 at 2:59
  • #3, not as much as you may think, certainly not in the web world anymore.
    – Kevin B
    Commented Apr 21, 2021 at 3:10
  • 2
    @NguyễnVănPhong #2 is implementation dependent. There is no "the engine" or a singular "behind the scenes" when we talk about these things. Arrays are to be treated however each implementation wants. They could just be exactly the same as objects. Although major engine vendors do actually optimise arrays. However, "how is it handled" requires extracting information from many different engines. And that's information that can change essentially tomorrow if Chrome or Firefox or whoever decides to add another optimisation to arrays.
    – VLAZ
    Commented Apr 21, 2021 at 5:15
4

The problem is that I actually come from C# strong type language, then I really was surprised with the strange things. After did google I didn't get the answer

This is because when coming to another language (especially from strongly-typed to weakly-typed and vice versa), it is useful to learn the basics of the one you are switching to (and the fact that arrays are "exotic objects" is part of the basics).

Granted, this is less well-known, so your confusion is somewhat warranted.

Especially, I have 4 concerns

You are lucky it wasn't closed as "too broad". Try to avoid asking multiple questions in one package (although in this case the subquestions are related to each other, so this is borderline).

get the 2 other people's opinions instead of using the privilege

They earned that privilege through a lot of positive contributions in the field ("Earn at least 1000 total score for at least 200 non-community wiki answers" from the badge description) and have a right to exercise it however they see fit (as long as they do not abuse it).

They gave you the correct suggestion in the comments: post on Meta and let the community weigh in.

They are totally different from the old question.

This may be true for the question, but we do not dupe close based on questions. Nor do we close in favor of older Q&As, though. It is the answers that matter. And, to be frank, both Q&As are subpar when it comes to answers.

In yours, there is at least a direct mention of the ECMAScript specification that explains the behavior in its entirety (whereas the dupe target's answers are written as if it does not exist). On the other hand, one of the answers cites w3schools as "a reputable source" (but we can help in curating that and other issues).

With that said, I voted to reopen the Q&A until either a more suitable target is found (with detailed answers relying on the language spec) or, in lieu of such Q&A be used as the target for other questions (as it at least has some info on the spec). Maybe we should create a canonical if there isn't one yet.

6
  • Thanks. I like this quote " as long as they do not abuse it". When we should change the flag from privilege to vote to close the question to get the other people's opinion. It's worth diving deeper as you said "Maybe we should create a canonical if there isn't one yet." Commented Apr 21, 2021 at 1:56
  • Well, I don't think they abused the privilege (and as far as I know, they can't "change" to vote-to-close even if they wanted). You are now here on Meta, we can discuss and possibly reopen your question. I (or someone more speedy) will search for a canonical - I can't believe we do not have a great Q&A explaining why arrays can be treated as objects.
    – 0Valt
    Commented Apr 21, 2021 at 2:05
  • 1
    Yes. Agree with you. Of course, I don't think they abused the privilege too. Just wanna discuss here to figure out the more satisfying decision. Commented Apr 21, 2021 at 2:08
  • 1
    @NguyễnVănPhong - just noting. My stance on this after reading both Q&As is to reopen yours, shapen it up (I've applied some fixes to the answers, but that's far from all that's needed) and close the original dupe target against yours. Unless we do have a canonical on the matter, in which case both Q&As will benefit from being closed against it (but yours still need to be reopen fist). Let's see what others think on the matter and let the peer review process take its due course. As you might've noticed, I also pinged Kevin that there is a discussion happening.
    – 0Valt
    Commented Apr 21, 2021 at 2:15
  • I'm not sure if dupe closing the old one as a dupe of this new one that easily falls under too broad is a good idea. The question is all over the place and the answers followed suit. For users who are simply asking why arrays can have string keys, the simpler question/answer pair is a far better pair to land on.
    – Kevin B
    Commented Apr 21, 2021 at 14:51
  • @KevinB - I actually do not disagree with you much. As I mentioned, both Q&As need some love and if you ask me, they both should be closed to a canonical that thoroughly explains why this is according to the language spec. The only thing I differ on is that the OPs Q&A should be closed in favor of the old one as it is arguably no better than the recent one (apart from conciseness), that's all.
    – 0Valt
    Commented Apr 21, 2021 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .