Home > Web Front-end > JS Tutorial > How Do I Create and Access 2D Arrays in JavaScript?

How Do I Create and Access 2D Arrays in JavaScript?

Patricia Arquette
Release: 2024-12-17 02:31:25
Original
940 people have browsed it

How Do I Create and Access 2D Arrays in JavaScript?

Creating and Accessing Two-Dimensional Arrays in JavaScript

Despite debates surrounding the existence of two-dimensional arrays in JavaScript, you can create an array of arrays to emulate their functionality. Here's how:

Declaring a 2D Array:

To declare a 2D array in JavaScript, define an array named myArray and assign to it an array of arrays, e.g.:

let myArray = [[1, 2], [3, 4], [5, 6]];
Copy after login

Accessing Members:

To access members of this 2D array, use nested indexing using square brackets, e.g.:

Accessing by Row and Column:

  • myArray[0][1]: Accesses the element in the first row (index 0) and second column (index 1) - returns 2.
  • myArray[1][0]: Accesses the element in the second row (index 1) and first column (index 0) - returns 3.

Retrieving the Entire Array:

  • myArray: Returns the entire two-dimensional array.

The above is the detailed content of How Do I Create and Access 2D Arrays in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template