Last active
January 13, 2017 11:40
-
-
Save andrei-cacio/3b9952b1811dc24aceb76461fbba9fe5 to your computer and use it in GitHub Desktop.
What will log? Part I
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var people = (function() { | |
var people = []; | |
render(); | |
function render() { | |
var people = people; | |
console.log(people); | |
} | |
}()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's
undefined
because of what's known as "hoisting" in javascript. When therender
function is called, an execution context is created in the execution stack. This happens in 2 phases, the first one being the "creation phase", where the interpreter finds all variable declarations and allocates memory space for them. It also and assigns them the special valueundefined
. The second phase, called the "execution phase", is when the interpreter actually executes the code and assigns the values. So it will assign the value ofpeople
(which isundefined
as explained in the "creation phase") topeople
, so it will be undefined.Easier said, it's like writing the code as:
var people;
people = people;