This is a module from free code camp that I copied. I was wondering how the values 13 & 14 are called since it’s a nested array. Would you do [3] [1] [0] to call value 13?
Any help is greatly appreciated
const arr = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[[10, 11, 12], 13, 14]
];
const subarray = arr[3];
const nestedSubarray = arr[3][0];
const element = arr[3][0][1];
This wasn’t covered in the module just trying to understand how arrays work
2
Answers
No, you would use [3][1] for 13 and [3][2] for 14.
The third element of arr is [[10, 11, 12], 13, 14], and the first and second elements of this are, 13 and 14, respectively.
No. Just
arr[3][1]
is enough to get the value13
. Andarr[3][2]
gets you14
.You could explore this expression for yourself by logging each part.
Why
13[0]
isundefined
…In JavaScript, when you try to do property access on a Number using
[]
you’ll find that the number13
is a thing called a Number instance which is an ordinary object but it doesn’t have a property called "0", so it just results inundefined
.