2

Let me create one simple array.

var a = [5,"e",7];

Is it safe to think that a[0] just knows the address of some value in memory, and not storing the value it self in that list ( because it some sense it is key value pair)? So if I do

a[0] = 4;

I'm creating 4 in memory and pointing to the new place in memory where that value is created, and abandoning the old reference to a memory (5) and that value (5) gets garbage-collected because nothing points to that spot of memory. Right?

Basically, can I say that a[0], a[1]... just knows the address of some value somewhere over there?

De-refferencing the values

  • 2
    JavaScript arrays are specialized JavaScript objects, and the indexed properties are really no different than any other sort of object property. – Pointy Jan 21 '18 at 19:12
  • take a look here: https://stackoverflow.com/questions/20321047/how-are-javascript-arrays-represented-in-physical-memory – Black.Jack Jan 21 '18 at 19:13
  • Maybe this will help you [Array vs. Object efficiency in JavaScript](https://stackoverflow.com/questions/17295056/array-vs-object-efficiency-in-javascript) – kabanus Jan 21 '18 at 19:14
  • 2
    You're asking if primitive values are boxed and stored on the heap instead of copied directly into the indexed location? This is ultimately an implementation dependent question, but I would imagine primitives are copied directly into the index location and are not behind a pointer. –  Jan 21 '18 at 19:16
  • 2
    Now what is the *actual* concern? What problem are you worried about running into? Dangling pointers? Use after free? Data races? Memory leaks? Some other memory issue? It would be helpful if you were more specific about the problem you're thinking of rather than asking about implementation details. –  Jan 21 '18 at 19:21
  • @rockstar My concern was what does `a[0]` actually means when you do that. – Learn on hard way Jan 21 '18 at 20:11
  • @Learnonhardway: It actually means give me the value at index `0` of array `a`. –  Jan 21 '18 at 21:43

0 Answers0