0

See this code:

var arr = [];
arr.foo = 'bar';
console.log(arr.foo);

Now, we see that arr.foo doesnt throw an error and works, but technically it should throw an error so why doesn't it?

Also, how is the above represented in memory, considering array blocks are mostly allocated memory in continuous location with the index of the offset, how does that work here?

3
  • "technically it should throw an error" Why should it? Nothing governs the behavior of a language except its specification or implementation if no spec exists. Commented Feb 24, 2018 at 18:26
  • but technically it should throw an error No, it shouldn't. You can add a new property to any object by simply assigning a value to a non-existent property. Arrays inherit from Object and so they do every thing that an Object does. Commented Feb 24, 2018 at 18:26
  • Everything is an object in javascript. You can refer to this SO question: stackoverflow.com/questions/9108925/…; Javascript is not like other untyped languages. You should also investigate on javascript's roots - as it was written in a very short time (1 week?) as sort of POC, and got out of hand from that point on to what we have today. ES6 and TypeScript finally start to make order in the chaos. The turn point was the release of Chrome V8 engine. Commented Feb 24, 2018 at 18:28

2 Answers 2

3

...technically it should throw an error...

No, it works entirely as described in the specification.

It works because standard JavaScript arrays aren't really arrays,* they're just objects backed by Array.prototype with a special length property and special handling for property names that are array indexes according to the specification. A property with any other name is just a normal object property, not an array entry.

Since arrays are objects, they can have non-array-entry properties, just like any other object.

FWIW, the definition of an array index is:

An integer index is a String-valued property key that is a canonical numeric String (see 7.1.16) and whose numeric value is either +0 or a positive integer ≤ 253-1. An array index is an integer index whose numeric value i is in the range +0 ≤ i < 232-1.

Note that typed arrays are true arrays; but they're also objects, and you can add non-array-entry properties to them, too.


* (that's a post on my anemic little blog)

Sign up to request clarification or add additional context in comments.

2 Comments

So the [] we see when console.logging an array is just some tweak built into the toString function?
@AyushGupta: No, it's something the console implementation does when it detects you're outputting an array. If you apply toString to an array, it calls .join(","). So for instance, [1, 2, 3].toString() returns "1,2,3".
0

Arrays are Objects and you can put inside different kinds of objects like strings, like dictionaries, another objects in general etc. when you write:

arr.foo="bar"

you are puting a value "bar" for the name access foo. your variable arr is now

 arr={foo:"bar"}

and you can access it like you did arr.foo if you want more information check te page from the w3c here https://www.w3schools.com/js/js_arrays.asp in the section array object.

3 Comments

Please don't link to W3 Schools. It's well-known to often be incomplete or inaccurate. Use MDN instead.
@ScottMarcus I didn't know it I mean for me they are the W3C. anyway there are a lot of information online. So, you can choose the best for your doubts Ayush.
W3 Schools is, in no way, related to the W3C (W3.org). It's widely known that W3 Schools is not the place to go for accurate and up to date information. This is not my personal opinion. Since JavaScript is maintained by The Mozilla Organization, the Mozilla Developer's Network is the authoritative source for information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.