0

I am new to JavaScript and lately, I found out that arrays in JavaScript are like lists in Java and that they can contain different types of variables.

My question is if in JavaScript an array are made of pointers? How is it possible to have different types in the same array, because we must define the array size before we assign the variables?

I have tried to find some information on Google, but all I have found are examples on arrays ):

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • [Did you look at this question?](https://stackoverflow.com/questions/365975/how-are-javascript-arrays-implemented) – Józef Podlecki May 18 '20 at 20:42
  • Arrays in javascript are objects.Check this https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – M A Salman May 18 '20 at 20:45
  • Yes, arrays are not (necessarily) "array datastructures". And yes, the only way to store data of dynamic size inside a container of fixed size is to allocate data somewhere else and store a pointer to that. – Jonas Wilms May 18 '20 at 21:45
  • Engines do however have various internal representation of arrays, so this question is quite hard to answer accurately. I strongly recommend reading through https://v8.dev to get some insights into whats happening under the hood of the engines. – Jonas Wilms May 18 '20 at 21:46

3 Answers3

1

You do not have to define the array size before you assign the variables. You can go like:

let array = [];
array.push(12);
array.push("asd");
array.push({data:5});
array.forEach(element => {
    console.log(element);
}); 

Also I think you should not think about pointers with such a high level language. The better way is to look at variables like 'primitives' and 'objects'. Here is a good read about it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

1

High level languages, and in particular scripting languages, tend to reference most things with pointers, and they make pointer access transparent. Javascript does this also. Most everything, even primitives like numbers and strings, are objects. Objects in javascript have properties that store things. Those properties are essentially pointers, in that they are references to other objects. Arrays are implemented in the same way, and are in fact objects with numeric properties (and a few utility methods a standard object doesn't have, such as .length, .push(), .map(), etc.). Arrays don't hav a fixed size anymore than objects do. So everything in javascript is stored in these object "buckets" that can store anything in their properties (although you can seal objects, like numbers and strings, so that they don't accidentally change).

Languages with fixed data types (C like languages for instance) implement things with fixed data structures, and the exact size is easily calculable and known. When you declare a variable, the compiler uses the type of that variable to reserve some space in memory. Javascript handles all that for you and doesn't assume anything is a fixed size, because it can't. The size of javascript objects can change at any time.

In C-Like languages, when you ask for an array, you are asking for a block of a specific size. The compiler needs to know how big that is so that it can determine where in memory to put everything, and it can use the type of objects in the array to easily calculate that. Interpreted languages use pointers behind the scenes to keep track of where everything is stored, because they can't assume it will always be in the same place, like a compiled program can. (This is somewhat of a simplification and there are caveats to this of course).

Garrett Motzner
  • 3,021
  • 1
  • 13
  • 30
-1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

JavaScript is a loosely typed language, Therefore there is noting stoping you from having different types in javascript array. but I would strongly avoid structuring your data that way without static type-checking (Typescript)

const test = ['test', {test:'test'}, 1, true]
ysf
  • 4,634
  • 3
  • 27
  • 29