Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in javascript, what happens if: x = []; x.push(x);?

var x = []; 
x.push(x);

x now seems to be an infinitely deep russian-doll type meta array.

if you check x[0][0][0].... as many [0] indexes as you add, it still returns a one-item array.

but is there a finite depth cutoff? or are new levels procedurally generated when you check? those are the only two possibilities I can think of.

like image 327
T. Rexman Avatar asked Sep 30 '22 15:09

T. Rexman


1 Answers

var x = []; 
x.push(x);

x now seems to be an infinitely deep russian-doll type meta array.

Not really. x is an array. The first element in the array points to the same memory location as x itself. Nothing more, nothing less.

But yes, you can do

x[0][0][0]

as many times as you like, since you're just re-referencing the same memory location over and over again.

like image 54
Adam Rackis Avatar answered Oct 14 '22 06:10

Adam Rackis