Categories
JavaScript Answers

How to create a two dimensional array in JavaScript?

Spread the love

To create a two dimensional array in JavaScript, we put arrays inside an array.

For instance, we write

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

to put arrays inside the items array.

Then we access the first array in item‘s items with

console.log(items[0][0]);
console.log(items[0][1]);

We access the 2nd array in item‘s items with

console.log(items[1][0]);
console.log(items[1][1]);

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.