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?

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92
  • *"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. –  Feb 24 '18 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. – Scott Marcus Feb 24 '18 at 18:26
  • Everything is an object in javascript. You can refer to this SO question: https://stackoverflow.com/questions/9108925/how-is-almost-everything-in-javascript-an-object; 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. – orberkov Feb 24 '18 at 18:28

2 Answers2

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)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • So the `[]` we see when `console.log`ging an array is just some tweak built into the `toString` function? – Ayush Gupta Feb 24 '18 at 18:28
  • @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"`. – T.J. Crowder Feb 24 '18 at 18:29
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.

  • 2
    Please don't link to W3 Schools. It's well-known to often be incomplete or inaccurate. Use [MDN](https://developer.mozilla.org) instead. – Scott Marcus Feb 24 '18 at 18:32
  • @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. – Angel Astorga Feb 24 '18 at 18:39
  • 2
    W3 Schools is, in no way, related to the W3C (http://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. – Scott Marcus Feb 24 '18 at 18:42