Q: Variable scope in function expressions I've been learning some basics of JavaScript lately and have spent quite some time disassembling some function expressions to better understand them. I'm still a bit unsure what actually happens in the few examples and would like some explanation on it if I'm wrong, or at least confirmation whether my train of thought is correct.
There are three snippets of code with slight differences in them:
var my_number = 7;

var timesTwo = function(number) {
    my_number = number * 2;
    console.log("inside " + my_number);
};

timesTwo(5);
console.log("outside " + my_number);

.
var my_number = 7;

var timesTwo = function(number) {
    var my_number = number * 2;
    console.log("inside " + my_number);
};

timesTwo(5);
console.log("outside " + my_number);

.
var my_number = 7;

var timesTwo = function(my_number) {
    my_number = my_number * 2;
    console.log("inside " + my_number);
};

timesTwo(5);
console.log("outside " + my_number);

What I'm curious about is what exactly happens to make them work like they do.
The first case seems relatively straight forward - my_number starts as a global, number is local to the function and my_number is changed inside the function so it's 10 in global frame.
The second case also seems clear - it's similar to the first one, only there is a new local variable my_number inside the function that is assigned 10, and outside the function my_number is still 7.
What I am unsure about is the third piece. I assume that since all mentions of number is now my_number, the function creates a new my_number variable in the following line:
var timesTwo = function(my_number)

even though there is no var my_number in the function body?

A: var my_number = 7;

var timesTwo = function(number) {
    my_number = number * 2;
    console.log("inside " + my_number);
};

timesTwo(5);
console.log("outside " + my_number);

This first example declares my_number in the global scope and sets its value to 7. timesTwo is a function that accepts an argument in its parameter, which is then referenced to as number within the function. When the function is called, you are setting the my_number variable defined in the global scope to be equal to the passed in argument multiplied by two. You are logging the same result with both the "inside" and "outside" calls.
var my_number = 7;

var timesTwo = function(number) {
    var my_number = number * 2;
    console.log("inside " + my_number);
};

timesTwo(5);
console.log("outside " + my_number);

In this second example, you are declaring a locally scoped my_number within the timesTwo function. Because of this, you are logging different my_number variables in your "inside" and "outside" calls.
var my_number = 7;

var timesTwo = function(my_number) {
    my_number = my_number * 2;
    console.log("inside " + my_number);
};

timesTwo(5);
console.log("outside " + my_number);

In the third example, timesTwo accepts a parameter and is using the variable my_number to keep track of it.

A: This is known as variable shadowing.
In the function definition you specify the my_number parameter which introduces a new local variable into the scope of the function.
This will shadow the outer my_number so that any reference to my_number within the function body will refer to the incoming parameter rather than the outer my_number variable.
