Create Arrays in JavaScript
5 min read·Jan 14, 2026
In JavaScript, arrays are resizable structures that can contain a mix of different data types ordered in a sequence.
Array literals are created using square brackets []:
array = [element1, ..., elementN];
Where:
element1, ..., elementNare a list of optional values or variables separated by a comma character (,).
📚 In JavaScript, the value contained in an array is referred to as an element.
Example
In this example, the array assigned to the todo variable contains 0 elements:
let todo = [];
In this example, the array assigned to the grades variable contains 4 integer elements:
let grades = [3, 8, 7, 6];
In this example, the array assigned to the students variable contains 2 object elements:
let students = [
{
name: 'Jack Daniels',
grades: [8, 7.5, 4]
},
{
name: 'Johnny Walker',
grades: [6, 9, 8]
}
];
Create multidimensional arrays
In JavaScript, a multidimensional array is an array that contains other arrays referred to as nested arrays.
array = [[], ...];
These structures are commonly used to describe real-world objects, such as matrices in mathematics, grids and maps in computer games, tables in spreadsheet-like data representations, and so on.
💡 Nested arrays don't have to have the same number of elements. When this happens, these arrays are referred to as jagged arrays.
Example
In this example, the multidimensional array assigned to the sheet variable contains 3 nested arrays of 2 elements each, that represents a spreadsheet with 3 columns and 2 rows:
const sheet = [
["Name", "Age", "Country"],
["Viktor", 32, "Canada"],
["Maya", 28, "Italy"],
];
This array can be represented in the text format as follows:
+--------+-----+---------+
| Name | Age | Country |
+--------+-----+---------+
| Viktor | 32 | Canada |
| Maya | 28 | Italy |
+--------+-----+---------+
In this example, the multidimensional array assigned to the board variable contains 3 nested arrays of 3 elements each, that represents the board of a tic-tac-toe game:
const board = [
[' ', ' ', 'X'],
['o', 'x', ' '],
['o', ' ', ' ']
];
This array can be represented in the text format as follows:
| | X
---|---|---
O | X |
---|---|---
O | |
Enjoying the courses?
I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:
Support BackendBrewery