Practically? Yes. You can create an array of arrays which functions as an 2D array as every item is an array itself:

let items = [
  [1, 2],
  [3, 4],
  [5, 6]
];
console.log(items[0][0]); // 1
console.log(items[0][1]); // 2
console.log(items[1][0]); // 3
console.log(items[1][1]); // 4
console.log(items);

But technically this is just an array of arrays and not a “true” 2D array, as I. J. Kennedy pointed out.

It should be noted that you could keep nesting arrays into one another and so create “multidimensional” arrays.

Answer from Ballsacian1 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-array-of-arrays
JavaScript Array of Arrays - GeeksforGeeks
August 5, 2025 - Here, each sub-array represents a group of related items. Sometimes, you might want to convert a nested array into a single array. This is known as flattening the array. JavaScript provides a convenient method flat() to achieve this.
🌐
W3Schools
w3schools.com › js › js_arrays.asp
JavaScript Arrays
Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays.
Discussions

How to create an array of arrays using a loop
have you tried reading the mdn docs on your array constructor? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Array you'e creating a new array, but you're putting the temp_array into a new array. You're not creating a new version of temp_array. this is what it should look like; const array_of_arrays = []; const loopOne = 5;//where loopOne is the random amount of times array_of_arrays is meant to loop? const loopTwo = 3;//the random length of the loop for the temp_array for( let i = 0; i < loopOne; i++ ){ let temp_array = []; for( let j = 0; j < loopTwo; j++ ){ temp_array.push(j); } array_of_arrays.push(temp_array); } console.log(array_of_arrays); More on reddit.com
🌐 r/learnjavascript
9
2
March 31, 2023
Can you only add an array to another array?
Push: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push const numbers = [1, 2, 3]; numbers.push(4); console.log(numbers); // [1, 2, 3, 4] const arrays = [[1, 2], [3, 4]]; arrays.push([5, 6]); console.log(arrays); // [[1, 2], [3, 4], [5, 6]] const whatever = [{ name: 'Sue' }, false, null]; whatever.push('eleventy'); console.log(whatever); // [{ name: 'Sue' }, false, null, 'eleventy'] Concat: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat const odds = [1, 3]; const evens = [2, 4]; const all = odds.concat(evens); console.log(odds); // [1, 3] console.log(evens); // [2, 4] console.log(all); // [1, 3, 2, 4] Spread operator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax const odds = [1, 3]; const evens = [2, 4]; const all = [...odds, ...evens]; console.log(odds); // [1, 3] console.log(evens); // [2, 4] console.log(all); // [1, 3, 2, 4] More on reddit.com
🌐 r/learnjavascript
9
6
October 4, 2022
How to create an array of arrays from existing single arrays

In principle:

const arrayOfArrays = [arr1, arr2, arr3];
More on reddit.com
🌐 r/learnjavascript
10
2
September 5, 2020
An array of arrays of arrays?
Absolutely. You can create multiple arrays and then store each of those collections into a new array. You could continue doing that, but it'd probably get messy, code wise, and potentially detrimental to performance if your data was structured that way. More on reddit.com
🌐 r/javahelp
9
2
July 24, 2019
🌐
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.
🌐
DEV Community
dev.to › byte-sized-news › creating-arrays-of-arrays-nested-arrays-in-javascript-common-pitfalls-and-best-practices-20de
Creating Arrays of Arrays (Nested Arrays) in JavaScript: Common Pitfalls and Best Practices - DEV Community
August 1, 2025 - In this post, we’ll walk through the right and wrong ways to create arrays of arrays in JavaScript, explain why certain pitfalls occur, and show you best practices for robust code.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array
Array - JavaScript - MDN Web Docs
July 28, 2026 - 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 ...
🌐
The Valley of Code
thevalleyofcode.com › lesson › js-arrays › arrays-of-arrays
Arrays: Arrays of arrays
Since we can add an array into an array, we can create multi-dimensional arrays, which have very useful applications (e.g.
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › of
Array.of() - JavaScript - MDN Web Docs
July 10, 2025 - (That implies an array of 7 empty slots, not slots with actual undefined values.) ... Array.of(7); // [7] Array(7); // array of 7 empty slots Array.of(1, 2, 3); // [1, 2, 3] Array(1, 2, 3); // [1, 2, 3]
🌐
Reddit
reddit.com › r/learnjavascript › how to create an array of arrays using a loop
r/learnjavascript on Reddit: How to create an array of arrays using a loop
March 31, 2023 -

SOLVED: I replaced this line:

audienceGroups.push(oneAudienceGroup);

with this line:

audience_groups.push(JSON.parse(JSON.stringify(one_audience_group)));

Works great now.

////////////////////////////////////

EDIT: Here's the actual code

The user checks boxes, and I need to store their choices.

This is the page I'm having trouble with. I'm using javascript and the DOM. The three niches (Productivity, Marketing, Sales) and the social media audiences are chosen by the user on previous pages and stored in arrays. I insert their choices into the DOM to create this page. (Ignore the word "undefined," it's not relevant to my current issue.) The group of audiences chosen is always repeated under whatever niches are chosen. The user chooses from long lists of niches and audiences, so I don't know ahead of time how many or which niches or audiences there will be.

When they see this page, nothing is checked. They decide which audiences to check under the niches.

I need to store the checked audiences under each niche in its own array (oneAudienceGroup), and push those arrays onto a larger array (audienceGroups). So based on the image, I need these three arrays:

Facebook, Instagram
Facebook, Instagram, LinkedIn, TikTok 
YouTube

and push them onto a larger array. And then I need to be able to retrieve the lists in the separate arrays. The problem is when I look inside audienceGroups, it seems to contain three references to the last verson of oneAudienceGroup. So it contains YouTube, YouTube, YouTube.

My question is: how do I make a copy of oneAudienceGroup to push onto audienceGroups?

Here's the structure of the HTML:

<ul id="matching_niches_audiences">
<li>
	<b>[NICHE]</b><br>
	<b>[undefined]</b>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
</li>
<br>
<li>
	<b>[NICHE]</b><br>
	<b>[undefined]</b>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
</li>
<br>
<li>
	<b>[NICHE]</b><br>
	<b>[undefined]</b>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
	<li>
		<input type="checkbox">
		[AUDIENCE]
	</li>
</li>
</ul>

Here's the javascript. I marked the problem line with asterisks. I've included what's getting logged to the console. Everything seems to be fine in the code except how to copy an array to the larger array:

const audienceGroups = [];
const oneAudienceGroup = [];
let nicheLi;
let audienceLis;
const allNicheLis = document.querySelector('#matching_niches_audiences').childNodes; 
    console.log(allNicheLis.length); // 3    number of niches
    
    for (let w = 0; w < allNicheLis.length; w++)
    {
        nicheLi = allNicheLis[w];
        audienceLis = nicheLi.getElementsByTagName('li');
        console.log(audienceLis.length); // 3 iterations: 5, 5, 5  the number of audiences displayed under each niche
        oneAudienceGroup.length = 0; 
        for (b = 0; b < audienceLis.length; b++)
        { 
            if (audienceLis[b].getElementsByTagName('input')[0].checked)
            {
                oneAudienceGroup.push(audienceLis[b].textContent);
            }
        }
        // This output is correct, group 1: Facebook Instagram,  group 2: Facebook Instagram LinkedIn TikTok, group 3: YouTube
        for (let j = 0; j < oneAudienceGroup.length; j++){console.log(oneAudienceGroup[j]);
        }
        // *** This line is the problem. I need to push a *** 
        // *** copy of oneAudienceGroup, not a reference. ***
        audienceGroups.push(oneAudienceGroup);
    }

This is the code I used to check the contents of the larger array. It contains YouTube, YouTube, YouTube.

console.log(audienceGroups.length); // 3
for (let f = 0; f < audienceGroups.length; f++)
{
    console.log(audienceGroups[f].length); //  1, 1, 1 All three arrays have only one element.
    for (let g = 0; g < audienceGroups[f].length; g++)
    {
        console.log(audienceGroups[f][g]); // YouTube, YouTube, YouTube --- The audiences for the first two niches weren't stored.
    }
}

Thanks in advance for any help or suggestions.

/////////////////////////////////////////////////////////////////////////

original post

////////////////////////////////////////////////////////////////////////

In my app I'm using two nested loops to create an array of arrays. The inner loop creates short arrays and pushes them onto the larger array. I never know how many iterations each loop will do. I need a way to copy the shorter array to the larger array. What I'm doing below seems to be using a reference, even though I'm using new Array(). How do I make a copy of the shorter array and add it to the larger array?

This code isn't working because when I push temp_array onto array_of_arrays, array_of_arrays ends up filled with references to the latest value of temp_array. I don't understand why new Array() doesn't fix the problem.

var temp_array = [];
var array_of_arrays = [];
temp_array.push(1); // push() happens in a loop. Number of iterations is unpredictable.
temp_array.push(2);
array_of_arrays.push(new Array(temp_array));
temp_array.length = 0;
temp_array.push(3);
temp_array.push(4);
temp_array.push(5);
temp_array.push(6);
array_of_arrays.push(new Array(temp_array));
temp_array.length = 0;
temp_array.push(7);
array_of_arrays.push(new Array(temp_array));
console.log(array_of_arrays.length); // 3
console.log(array_of_arrays[0].length); // 1
console.log(array_of_arrays[1].length); // 1
console.log(array_of_arrays[2].length); // 1
console.log(array_of_arrays[0][0]); // 7
console.log(array_of_arrays[1][0]); // 7
console.log(array_of_arrays[2][0]); // 7
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › Array
Array() constructor - JavaScript - MDN Web Docs
If the only argument passed to the Array constructor is an integer between 0 and 232 - 1 (inclusive), this returns a new JavaScript array with its length property set to that number. Note: This implies an array of arrayLength empty slots, not slots with actual undefined values — see sparse arrays).
🌐
Code Institute
codeinstitute.net › blog › javascript › javascript arrays
Javascript Arrays: A Guide - Code Institute Global
December 20, 2022 - Since JavaScript arrays do not refer to associative arrays, it is impossible to access array items using random strings as indexes; instead, nonnegative numbers (or their corresponding string forms) must be used. Square brackets are used to indicate how to obtain an array’s elements. The array’s elements are each given an index. An array’s first index is always zero by default. The index value is enclosed in a set of square brackets, as in veggies [0], to retrieve a specific element from an array.
🌐
Codecademy
codecademy.com › docs › javascript › arrays
JavaScript | Arrays | Codecademy
March 20, 2024 - Returns a copy of an array with the given modification. ... Creates a new array with all sub-array elements recursively concatenated into it up to the specified depth. ... Applies a reducer function to array elements from right to left, accumulating a single output value. Preview: @StevenSwiniarski 473 total contributions ... Front-end engineers work closely with designers to make websites beautiful, functional, and fast. ... Learn how to use JavaScript ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-arrays
JavaScript Arrays - GeeksforGeeks
Arrays can hold any type of data-such as numbers, strings, objects, or even other arrays-making them a flexible and essential part of JavaScript programming.
Published: 3 weeks ago
🌐
DEV Community
dev.to › sanchithasr › understanding-nested-arrays-2hf7
Understanding Nested Arrays in JavaScript - DEV Community
January 5, 2021 - The arrays are listed according to the index. Below diagram explains how the indices are assigned to the elements in Nested Array. Say we want to access the value ‘Tangled’, we can navigate to it using this table.
🌐
DigitalOcean
digitalocean.com › community › tutorials › understanding-arrays-in-javascript
Understanding Arrays in JavaScript | DigitalOcean
In this tutorial, we will learn how to create arrays; how they are indexed; how to add, modify, remove, or access items in an array; and how to loop through …
🌐
Codecademy
codecademy.com › article › learn-javascript-arrays-arrays
Javascript Guide: Arrays | Codecademy
Arrays are JavaScript’s ordered lists that can store any data types including Strings, Numbers, and Booleans. Each item inside of an array is at a numbered position.
🌐
Launch School
launchschool.com › books › javascript › read › arrays
Understand JavaScript Arrays and Common Array Methods with Clarity
You must be careful to distinguish between these two things. push appends elements to the end of the caller array, but it returns the array's updated length. Note that it does not return the modified array! New JavaScript programmers often get confused over this difference and spend hours puzzling ...
🌐
Sentry
sentry.io › sentry answers › javascript › how can i create a two-dimensional array in javascript?
How can I create a two-dimensional array in JavaScript? | Sentry
February 15, 2023 - The Array() constructor can be called with or without the new keyword. Both constructions create an array instance. Youtube How Sentry.io saved me from disaster (opens in a new tab) Resources Improve Web Browser Performance - Find the JavaScript code causing slowdowns (opens in a new tab)
🌐
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.