1

I use the term loosely as an array in JavaScript can look like this:

let array = [1, 'a', "hello"];

If I do array.push('end') I get

[1, 'a', "hello", "end"]

An array in JavaScript seems to have nothing to do with the arrays taught in Computer Science were all the items are of the same type and this makes it easy to access by index as simple math can be used to determine where each index is in memory.

My assumption would be a single linked list with data being a reference to another object.

It would be cool to see the code if someone knows where it is for the V8 engine.

jennifer
  • 682
  • 5
  • 14
  • https://tc39.es/ecma262/#sec-array-objects – Thalaivar May 09 '20 at 16:22
  • 3
    Every language has different implementation of its datatypes.This is how it is designed. – Shubham Dixit May 09 '20 at 16:23
  • well, javascript is a weakly typed language, of course you can have arrays of mixed types. if you want something strongly typed, checkout typescript. don't call javascript arrays "improper" arrays. They are arrays. an array is a simple container implementing an iterable interface, and as such there can be various types of arrays, javascripts are numeric associative any[] type arrays. – r3wt May 09 '20 at 16:33
  • 1
    @r3wt "The term array is often used to mean array data type, a kind of data type provided by most high-level programming languages that consists of a collection of values or variables that can be selected by one or more indices computed at run-time. Array types are often implemented by array structures; however, in some languages they may be implemented by hash tables, linked lists, search trees, or other data structures." TLDR: The OP is correct, if we speak about [array data structures](https://en.m.wikipedia.org/wiki/Array_data_structure) – Jonas Wilms May 09 '20 at 16:36
  • "*My assumption would be a single linked list*" - on what is this assumption based? No, the js `Array` data structure is what is known as a "variable sized array", "vector" or deque in other languages, and is normally implemented as such. – Bergi May 09 '20 at 16:38
  • @r3wt also typescript is not strongly typed. – Jonas Wilms May 09 '20 at 16:38
  • @Bergi - I meant guess, if an object is implemented as a hash table, then it would make sense that an array could just as easily be implemented as a hash table, on second thought. computer science arrays are the fastest for lookup and next fastest would be hash tables. so this makes sense. – jennifer May 09 '20 at 16:41
  • @Bergi - Many of these answers are nearly 10 years old and messy, some of the links are broken as well. SO needs a way to not mark everything as a duplicate, I mean how many years till an answer is obsolete? – jennifer May 09 '20 at 16:45
  • @j.a. [This answer](https://stackoverflow.com/a/11433793/1048572) doesn't seem outdated at all, despite its age. – Bergi May 09 '20 at 16:49

1 Answers1

7

The source can be found here for V8. Currently V8 implements arrays in two ways:

 // The JSArray describes JavaScript Arrays
 // Such an array can be in one of two modes:
 //    - fast, backing storage is a FixedArray and length <= elements.length();
 //       Please note: push and pop can be used to grow and shrink the array.
 //    - slow, backing storage is a HashTable with numbers as keys.

So arrays are currently implemented as hashtables or array lists. This has changed in the past, and might change in the future. Also it may vary for other engines.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151