Defining 2D array Javascript

The variable leds needs to be defined as an array before it can be used as one. Then you can add elements to it using the push method; and these elements can be other (nested) arrays. For example:

//set leds as an empty array
let leds = [];

//add to the array
leds.push([]);
leds[0] = [1,2,3];

leds.push([]);
leds[1] = [4,5,6];

leds.push([7,8,9]);

//change an array value
leds[1][1] = 0;

console.log(leds);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top