2

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.

9
  • tc39.es/ecma262/#sec-array-objects Commented May 9, 2020 at 16:22
  • 3
    Every language has different implementation of its datatypes.This is how it is designed. Commented May 9, 2020 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. Commented May 9, 2020 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 Commented May 9, 2020 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. Commented May 9, 2020 at 16:38

1 Answer 1

8

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.

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

2 Comments

Nice reference. Is that the C programming language I see in the .h file?
@j.a. C++, yes ...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.