My basic understanding is that JavaScript runs from top to bottom, returning to functions when they are called, but then proceeding to the next line of code after calling a function.
That makes sense when I look at this code as an example:
var a = 5;
var b = a;
a = 2;
console.log(b);
// My expected output 5, works as expected
I get lost when I look at this though:
var a = [5];
var b = a;
a.push(2);
console.log(b);
// My expected output [5], instead returns [5,2]
If I assigned the var b the value of a when it was 5, why does the value of b change to [5,2] when 2 wasn't pushed to a until the NEXT line?
I'm obviously missing some basic fundamentals here.
bto the value ofaat that time, you're pointingbto the array thatais referencing, so any mutation to that array will be visible to all variables pointing to that object/array.aandbare pointing to the same value in memory, so every change onawill modifyb.