How to Declare Two Dimensional Empty Array in JavaScript ?
Last Updated :
24 May, 2024
In this article, we are going to learn about Declare two-dimensional empty array by using JavaScript. A two-dimensional array is also known as a 2D array. It is a data structure in JavaScript that can hold values in rows and columns form. It is an array of arrays. Each element in a 2D array is accessible using two indices, and it is represented as an array[rowIndex][columnIndex].
There are several methods that can be used to add elements to Declare an empty two-dimensional array in JavaScript, which are listed below:
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using a Loop
In this approach, we are using nested loops to iterate over the rows and columns of the 2D array and initialize each element to a specific value as null or undefined.
Example: In this example, the Empty2DArray function generates a 2D array with the specified number of rows (3) and columns (4) filled with null values. The arr1 is the resulting array.
JavaScript
function Empty2DArray(rows, cols) {
const array = [];
for (let i = 0; i < rows; i++) {
array[i] = [];
for (let j = 0; j < cols; j++) {
array[i][j] = null;
}
}
return array;
}
const arr1 = Empty2DArray(3, 4);
console.log(arr1);
Output[
[ null, null, null, null ],
[ null, null, null, null ],
[ null, null, null, null ]
]
JavaScript provides the Array() constructor, which allows us to create an array of a specific length. We can use this constructor to create a 2D array by initializing each element as an empty array.
function createEmpty2DArray(rows, cols) {
return Array.from({ length: rows }, () => Array(cols).fill(null));
};
Example: In this example, the Empty2DArray function uses Array.from() method to create a 2D array with 3 rows and 5 columns, initializing all elements to null. The arr2 holds the result.
JavaScript
function Empty2DArray(rows, cols) {
return Array.from({ length: rows },
() => Array(cols).fill(null));
}
let arr2 = Empty2DArray(3, 5);
console.log(arr2);
Output[
[ null, null, null, null, null ],
[ null, null, null, null, null ],
[ null, null, null, null, null ]
]
Approach 3: Using Array.from() with a Mapping Function
Using Array.from() with a mapping function, the code creates a two-dimensional array by specifying the number of rows and columns. Each cell is initialized using the mapping function to fill the array with a specified initial value.
Example
JavaScript
let rows = 3;
let cols = 4;
let initialValue = undefined;
let arr = Array.from({ length: rows }, () => Array(cols).fill(initialValue));
// Example usage
console.log(arr);
Output[
[ undefined, undefined, undefined, undefined ],
[ undefined, undefined, undefined, undefined ],
[ undefined, undefined, undefined, undefined ]
]
Similar Reads
How to declare a Two Dimensional Array of pointers in C? A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element. How to create a 2D array of pointers: A 2D array of pointers can be created follo
3 min read
How to Declare an Array in JavaScript? Array in JavaScript are used to store multiple values in a single variable. It can contain any type of data like - numbers, strings, booleans, objects, etc. There are varous ways to declare arrays in JavaScript, but the simplest and common is Array Litral Notations. Using Array Literal NotationThe b
3 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 min read
How to define custom Array methods in JavaScript? JavaScript arrays are versatile data structures used to store collections of items. When you want to iterate through the elements of an array, it's common to use loops like for, for...of, or forEach. However, there's a nuance when it comes to iterating through arrays that have additional properties
2 min read
How to Empty an Array in JavaScript? To empty an array in JavaScript, we can use the array literal. We can directly assign an empty array literal to the variable, it will automatically remove all the elements and make the array empty.1. Empty Array using Array LiteralWe can empty the array using the empty array literal syntax. Syntaxar
2 min read
Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J
3 min read
How to Define an Array with Conditional Elements in JavaScript? Sometime we may need to define an array with elements that depend on certain conditions. Such situation may occur when we have to dynamically generate arrays based on runtime logic such as including or excluding elements based on specific criteria. In this article, we'll explore various ways to defi
2 min read
How to Declare an Empty Array in TypeScript? TypeScript provides a robust type system that allows you to define and manage data structures more effectively than in plain JavaScript. One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to dec
2 min read
How to add elements to an existing array dynamically in JavaScript ? An array is a JavaScript object that can hold multiple values at a time, irrespective of the data type. In this article, we will see how to add new elements to an existing array dynamically in Javascript. We will discuss two methods in this article i.e. push() method and dynamically adding an elemen
4 min read
How Dynamic Arrays Work in JavaScript ? A dynamic array, also known as a resizable array or a dynamic array list, is a data structure that allows its size to be changed dynamically during program execution. Unlike static arrays, which have a fixed size determined at the time of declaration, dynamic arrays can grow or shrink in size as nee
3 min read