Statically typed languages care a lot about the things that are in the array being of the same type. Dynamically typed languages? Not so much. You can imagine JS arrays as being arrays of references to stuffs ^^ Answer from corpsmoderne on reddit.com
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript | MDN
2 days ago - The Array object, as with arrays in other programming languages, enables storing a collection of multiple items under a single variable name, and has members for performing common array operations. In JavaScript, arrays aren't primitives but are instead Array objects with the following core characteristics:
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
You should use arrays when you want the element names to be numbers. JavaScript has a built-in array constructor new Array().
Discussions

JavaScript arrays arent actually arrays at all?
Statically typed languages care a lot about the things that are in the array being of the same type. Dynamically typed languages? Not so much. You can imagine JS arrays as being arrays of references to stuffs ^^ More on reddit.com
🌐 r/learnprogramming
78
47
February 12, 2026
What’s the difference between “{}” and “[]” while declaring a JavaScript array? - Stack Overflow
Object properties can be accessed either via the x.foo syntax or via the array-like syntax x["foo"]. The advantage of the latter syntax is that you can use a variable as the property name like x[myvar] and using the latter syntax, you can use property names that contain characters that Javascript ... More on stackoverflow.com
🌐 stackoverflow.com
Javascript array becomes an object structure - Stack Overflow
I'm experiencing an odd behavior (maybe it isn't odd at all but just me not understanding why) with an javascript array containing some objects. Since I'm no javascript pro, there might very well be More on stackoverflow.com
🌐 stackoverflow.com
How do javascript arrays work under the hood?
The answer is that it depends on how you're using the array and what is in the array, and even then, it depends on the JavaScript engine that is executing the code. In V8, for example, if your array only contains integers, it'll be backed by a C++ array of integers. Typically, the backing array will be bigger than the number of integers it currently contains. If it contains a mixture of integers and floating point values or only floating point values, it'll be backed by an array of doubles. If it contains only objects, or a mixture of numbers and objects, it'll backed by an array of pointers. Even though JavaScript itself doesn't have a concept of 'integer' or 'double' - it just sees them all as 'number', V8 keeps track and makes it so arrays are a bit faster and more memory efficient if you only put integers in them. If you call push() when the backing array is full, it'll allocate a new, bigger backing array, copy the existing elements over, and then add the new value you pushed. This is similar to the implementation of ArrayList in Java or vector in C++. All of the above only is only sure to apply if your array is packed, and not sparse - i.e. you don't have any gaps in the array. If you do something like let abc = [1,2,3]; abc[100] = 50; you now have a sparse array. If is not too spare, it'll still be backed by an array, with empty array indices replaced with a 'hole' value. If you look at V8's C++ array source (linked below), you'll see calls to element->is_the_hole(i). If an array is very sparse, it'll no longer be backed by an array in memory. Instead, it will be backed by a dictionary/hashtable, and it'll take longer to both access elements and iterate through the array. If you're interested, you can read through V8's array implementation in C++ here . You'll notice that it often checks the following constants: PACKED_SMI_ELEMENTS - a packed integer array PACKED_DOUBLE_ELEMENTS - a packed double array PACKED_ELEMENTS - a packed object array HOLEY_SMI_ELEMENTS - a sparse integer array HOLEY_DOUBLE_ELEMENTS - a sparse double array HOLEY_ELEMENTS - a sparse object array DICTIONARY_ELEMENTS - a very sparse array that is backed by a dictionary And you'll see that it always tries to do whatever will be fastest for the array it is operating on. Lots of builtin functions like push, pop, shift, unshift, and concat do different things depending on the array's density and what kind of elements it contains. Some other things to keep in mind: if you have an array that only contains integers, and you push a floating point number or other type into it, it will be 'downgraded' for the rest of its life, even if you purge the non integers from it. Also keep in mind that none of these implementation details are guaranteed. A naive implementation of JavaScript's Array object could be backed by a linked list, and it would still work the same way it does now. It would just be slower. Actually, if you grab an early copy of the Mozilla source code from 20 years ago, you'll find that arrays were backed by ordinary JS objects without much optimization, just some extra code to handle special cases like the `length` property. More on reddit.com
🌐 r/javascript
11
18
November 29, 2018
🌐
JavaScript.info
javascript.info › tutorial › the javascript language › data types
Arrays
An array is a special kind of object. The square brackets used to access a property arr[0] actually come from the object syntax. That’s essentially the same as obj[key], where arr is the object, while numbers are used as keys.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-arrays
JavaScript Arrays - GeeksforGeeks
In JavaScript, an array is an ordered list of values.
Published   1 month ago
🌐
W3Schools
w3schools.com › js › js_array_methods.asp
JavaScript Array Methods
Many languages allow negative bracket indexing like [-1] to access elements from the end of an object / array / string. This is not possible in JavaScript, because [] is used for accessing both arrays and objects.
🌐
Mozilla
developer.mozilla.org › en-US › docs › Web › JavaScript › Guide › Indexed_collections
Indexed collections - JavaScript | MDN
For example, consider an array called emp, which contains employees' names indexed by their numerical employee number. So emp[0] would be employee number zero, emp[1] employee number one, and so on. JavaScript does not have an explicit array data type. However, you can use the predefined Array ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Learn_web_development › Core › Scripting › Arrays
Arrays - Learn web development | MDN
May 21, 2026 - In this lesson we'll look at arrays — a neat way of storing a list of data items under a single variable name. Here we look at why this is useful, then explore how to create an array, retrieve, add, and remove items stored in an array, and more besides.
🌐
Reddit
reddit.com › r/learnprogramming › javascript arrays arent actually arrays at all?
r/learnprogramming on Reddit: JavaScript arrays arent actually arrays at all?
February 12, 2026 -

So I have been learning computer science in college and getting specialized in web development just so I can get a better chance of landing an entry level job and I ran across something that I have been confused about. So in my understanding from my CS courses, an array is a contiguous composite data structure which holds homogeneous values which are ordered with an index. However in JS, arrays are composite data structures which hold heterogeneous values and are ordered with an index. Would an array in JS be closer to a record as far as data structures go or am I putting the cart before the horse in the importance of the allowance of more than one data structure? Is it more important that arrays are index-based by their definition more than it is important that they are homogeneous?

Any and all help would be great, thanks!!

Top answer
1 of 8
129

Nobody seems to be explaining the difference between an array and an object.

[] is declaring an array.

{} is declaring an object.

An array has all the features of an object with additional features (you can think of an array like a sub-class of an object) where additional methods and capabilities are added in the Array sub-class. In fact, typeof [] === "object" to further show you that an array is an object.

The additional features consist of a magic .length property that keeps track of the number of items in the array and a whole slew of methods for operating on the array such as .push(), .pop(), .slice(), .splice(), etc... You can see a list of array methods here.

An object gives you the ability to associate a property name with a value as in:

var x = {};
x.foo = 3;
x["whatever"] = 10;
console.log(x.foo);      // shows 3
console.log(x.whatever); // shows 10

Object properties can be accessed either via the x.foo syntax or via the array-like syntax x["foo"]. The advantage of the latter syntax is that you can use a variable as the property name like x[myvar] and using the latter syntax, you can use property names that contain characters that Javascript won't allow in the x.foo syntax.

A property name can be any string value.


An array is an object so it has all the same capabilities of an object plus a bunch of additional features for managing an ordered, sequential list of numbered indexes starting from 0 and going up to some length. Arrays are typically used for an ordered list of items that are accessed by numerical index. And, because the array is ordered, there are lots of useful features to manage the order of the list .sort() or to add or remove things from the list.

2 of 8
19

When you declare

var a=[];

you are declaring a empty array.

But when you are declaring

var a={};

you are declaring a Object .

Although Array is also Object in Javascript but it is numeric key paired values. Which have all the functionality of object but Added some few method of Array like Push,Splice,Length and so on.

So if you want Some values where you need to use numeric keys use Array. else use object. you can Create object like:

var a={name:"abc",age:"14"}; 

And can access values like

console.log(a.name);
🌐
Medium
medium.com › @teamtechsis › master-javascript-arrays-13e06b9af769
Master JavaScript Arrays. All-in-one starter guide to JavaScript… | by Natasha Ferguson | Medium
November 25, 2022 - An Array object is a JavaScript built-in object that can store multiple items of various data types in individual elements. An array is created by assigning [ ] square brackets to a variable which can optionally contain a comma-separated list ...
🌐
Code Institute
codeinstitute.net › blog › javascript › javascript arrays
Javascript Arrays: A Guide - Code Institute Global
December 20, 2022 - JavaScript arrays are types of objects that are data collections, but you should think of them more as displays. This guide has you covered.
🌐
Programiz
programiz.com › javascript › array
JavaScript Array (with Examples)
In JavaScript, an array is an object that can store multiple values at once. In this tutorial, you will learn about JavaScript arrays with the help of examples.
🌐
freeCodeCamp
freecodecamp.org › news › javascript-array-handbook
JavaScript Array Handbook – Learn How JS Array Methods Work With Examples and Cheat Sheet
August 31, 2023 - We'll cover the specific rules ... ... In JavaScript, an array is implemented as an object that can have a group of items, elements, or values as an ordered collection....
🌐
Medium
davitdvalashvili1996.medium.com › javascript-array-methods-your-complete-guide-372b9c6f12cd
JavaScript Array Methods: Your Complete Guide | by DavitDvalashvili | Medium
May 18, 2024 - In JavaScript, an array is a special type of object used to store and organize multiple values. Arrays enable you to group values under a single variable name, facilitating the management and manipulation of data collections.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-declare-an-array-in-javascript-creating-an-array-in-js
How to Declare an Array in JavaScript – Creating an Array in JS
November 7, 2024 - It stores multiple values and elements in one variable. These values can be of any data type — meaning you can store a string, number, boolean, and other data types in one variable....
🌐
Medium
medium.com › @erickanginyi › all-about-javascript-arrays-97292ef9a785
All About (JavaScript) Arrays
September 17, 2021 - An array can be defined as an ordered, list-like data structure. They’re a special type of JavaScript object that is represented by square brackets [], with the indexed data inside being separated by commas.
Top answer
1 of 2
8

The very last line might be causing the problem, have you tried replacing myArray[myArray.length] = obj; with myArray.push(obj);? Could be that, since you're creating a new index explicitly, the Array is turned into an object... though I'm just guessing here. Could you add the code used by the child document that retrieves myArray ?

Edit

Ignore the above, since it won't make any difference. Though, without wanting to boast, I was thinking along the right lines. My idea was that, by only using proprietary array methods, the interpreter would see that as clues as to the type of myArray. The thing is: myArray is an array, as far as the parent document is concerned, but since you're passing the Array from one document to another, here's what happens:

An array is an object, complete with it's own prototype and methods. By passing it to another document, you're passing the entire Array object (value and prototype) as one object to the child document. In passing the variable between documents, you're effectively creating a copy of the variable (the only time JavaScript copies the values of a var). Since an array is an object, all of its properties (and prototype methods/properties) are copied to a 'nameless' instance of the Object object. Something along the lines of var copy = new Object(toCopy.constructor(toCopy.valueOf())); is happening... the easiest way around this, IMO, is to stringency the array withing the parent context, because there, the interpreter knows it's an array:

//parent document
function getTheArray(){ return JSON.stringify(myArray);}
//child document:
myArray = JSON.parse(parent.getTheArray());

In this example, the var is stringified in the context that still treats myArray as a true JavaScript array, so the resulting string will be what you'd expect. In passing the JSON encoded string from one document to another, it will remain unchanged and therefore the JSON.parse() will give you an exact copy of the myArray variable.

Note that this is just another wild stab in the dark, but I have given it a bit more thought, now. If I'm wrong about this, feel free to correct me... I'm always happy to learn. If this turns out to be true, let me know, too, as this will undoubtedly prove a pitfall for me sooner or later

2 of 2
1

Check out the end of this article http://www.karmagination.com/blog/2009/07/29/javascript-kung-fu-object-array-and-literals/ for an example of this behavior and explanation.

Basically it comes down to Array being a native type and each frame having its own set of natives and variables.

From the article:

// in parent window
var a = [];
var b = {};
//inside the iframe
console.log(parent.window.a); // returns array
console.log(parent.window.b); // returns object

alert(parent.window.a instanceof Array); // false
alert(parent.window.b instanceof Object); // false
alert(parent.window.a.constructor === Array); // false
alert(parent.window.b.constructor === Object); // false

Your call to JSON.stringify actually executes the following check (from the json.js source), which seems to be failing to specify it as an Array:

        // Is the value an array?
        if (Object.prototype.toString.apply(value) === '[object Array]') {
           //stringify
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › javascript-array
What is a Javascript Array? Explained with Examples
July 17, 2023 - The JavaScript Array is a fundamental Data Structure that lets developers to store and manipulate collections of values in a single variable. Versatile and powerful, arrays can handle various types of data, including numbers, strings, objects, ...
🌐
Reddit
reddit.com › r/javascript › how do javascript arrays work under the hood?
r/javascript on Reddit: How do javascript arrays work under the hood?
November 29, 2018 -

When I used to program in basic or c, arrays were such a hassle because you had to declare the size of the array at the start. You could dynamically change the size, but I remember that being a hassle which I would do rarely.

But in javascript, I'm constantly pushing and pulling from arrays, and I'm never freeing up data or dealing with pointers or anything. I think you can even have different data types in the same array!

What is actually going on under the hood with javascript arrays?

Top answer
1 of 4
35
The answer is that it depends on how you're using the array and what is in the array, and even then, it depends on the JavaScript engine that is executing the code. In V8, for example, if your array only contains integers, it'll be backed by a C++ array of integers. Typically, the backing array will be bigger than the number of integers it currently contains. If it contains a mixture of integers and floating point values or only floating point values, it'll be backed by an array of doubles. If it contains only objects, or a mixture of numbers and objects, it'll backed by an array of pointers. Even though JavaScript itself doesn't have a concept of 'integer' or 'double' - it just sees them all as 'number', V8 keeps track and makes it so arrays are a bit faster and more memory efficient if you only put integers in them. If you call push() when the backing array is full, it'll allocate a new, bigger backing array, copy the existing elements over, and then add the new value you pushed. This is similar to the implementation of ArrayList in Java or vector in C++. All of the above only is only sure to apply if your array is packed, and not sparse - i.e. you don't have any gaps in the array. If you do something like let abc = [1,2,3]; abc[100] = 50; you now have a sparse array. If is not too spare, it'll still be backed by an array, with empty array indices replaced with a 'hole' value. If you look at V8's C++ array source (linked below), you'll see calls to element->is_the_hole(i). If an array is very sparse, it'll no longer be backed by an array in memory. Instead, it will be backed by a dictionary/hashtable, and it'll take longer to both access elements and iterate through the array. If you're interested, you can read through V8's array implementation in C++ here . You'll notice that it often checks the following constants: PACKED_SMI_ELEMENTS - a packed integer array PACKED_DOUBLE_ELEMENTS - a packed double array PACKED_ELEMENTS - a packed object array HOLEY_SMI_ELEMENTS - a sparse integer array HOLEY_DOUBLE_ELEMENTS - a sparse double array HOLEY_ELEMENTS - a sparse object array DICTIONARY_ELEMENTS - a very sparse array that is backed by a dictionary And you'll see that it always tries to do whatever will be fastest for the array it is operating on. Lots of builtin functions like push, pop, shift, unshift, and concat do different things depending on the array's density and what kind of elements it contains. Some other things to keep in mind: if you have an array that only contains integers, and you push a floating point number or other type into it, it will be 'downgraded' for the rest of its life, even if you purge the non integers from it. Also keep in mind that none of these implementation details are guaranteed. A naive implementation of JavaScript's Array object could be backed by a linked list, and it would still work the same way it does now. It would just be slower. Actually, if you grab an early copy of the Mozilla source code from 20 years ago, you'll find that arrays were backed by ordinary JS objects without much optimization, just some extra code to handle special cases like the `length` property.
2 of 4
3
In javascript, arrays are list-like objects. The indices of an array are essentially the keys/value pairs of an object. The global Array object prototype also has a bunch of built in methods that allow for traversing and manipulating the array. As for freeing up memory and stuff, javascript uses a process called garbage collection. From there, it's all magic to me at the moment. Sources: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array https://blog.sessionstack.com/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec