Skip to content

Instantly share code, notes, and snippets.

@andrei-cacio
Last active January 13, 2017 11:40
Show Gist options
  • Select an option

  • Save andrei-cacio/3b9952b1811dc24aceb76461fbba9fe5 to your computer and use it in GitHub Desktop.

Select an option

Save andrei-cacio/3b9952b1811dc24aceb76461fbba9fe5 to your computer and use it in GitHub Desktop.
What will log? Part I
var people = (function() {
var people = [];
render();
function render() {
var people = people;
console.log(people);
}
}())
@ionelh

ionelh commented Jan 13, 2017

Copy link
Copy Markdown

It's undefined because of what's known as "hoisting" in javascript. When the render 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 value undefined. 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 of people (which is undefined as explained in the "creation phase") to people, so it will be undefined.

Easier said, it's like writing the code as:
var people;
people = people;

@andrei-cacio

Copy link
Copy Markdown
Author

yes @ionelh, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment