Yes, you’re right, since 2
is not an array, it gets pushed to myArr
.But which one ?
Each time steamrollArray
(or any function, actually) is called, a new context is created.Think of it as creating a new tiny universe.
So this is how the story begins :
At line 20, when you call steamrollArray([1,[2],[[3]]])
, you create a universe.Let’s call it universe A.
Within this universe A, you are using the var
keyword on myArr, which means you’re creating a new variable called myArr
, and this variable belongs to universe A.
then at line 13, you call steamrollArray
again.You’re creating a new tiny universe, inside universe A! Let’s call it universe B.
Within universe B, because the keyword var
is used on myArr
, you’re also creating a new variable, that belongs to universe B, and is completely different from the myArr
from universe A!
The loop and if statement within universe B will push 2
to myArr
(the one from universe B), and in the end, universe B returns myArr
(which is [2]
), and just vanishes.No more universe B. The value [2]
is then sent to universe A subArray
variable.
You already know the end of the story. myArr
from universe A will concatenate [2]
, giving [1,2]
.And after that, the same thing will happen with [[3]]
, you will call steamrollArray
again, create universe C, then universe D inside universe C,…
One important thing to understand here is, if you use the var
keyword within a function, you are creating a new variable for this function.In this case, with the use of recursion, you’re not overriding other variables by using the same variable name, because you’re not declaring them in the same context.
One other thing to understand on the subject is (see my example below), within universe B(a nested function) you could have access to universe A(the wrapping context) variables.The universe A scope is available to universe B, because universe B is inside universe A.
One last thing: while you could access universe A scope from inside universe B, universe A cannot have access to universe B variables.
var name = "jack";
var name = "mike";
console.log(name); // mike
function someFunction() {
var name = "bill";
var billAge = " 'bill is 20' ";
function nestedFunction() {
var name = "tom";
var gender = "male";
console.log(name, billAge);
}
nestedFunction(); // tom 'bill is 20'
console.log(name); // bill
console.log(gender); // ReferenceError: gender is not defined
}
someFunction();
update :
Here’s another example to help you :
function someFunction() {
var name = "sam";
function anotherFunction() {
var name = "john";
yetAnotherFunction(); // sam
}
function yetAnotherFunction() {
console.log(name);
}
anotherFunction();
}
someFunction();