The Closure Compiler has several optimization that only run within function bodies (because they are too expensive to run on large projects): "flow sensitive inlining" is the one you want here. In simple mode, this:
function f() {
var a = 1;
a = 2;
window.console.log(a);
}
becomes:
function f() {
window.console.log(2);
}
There isn't anything you need to do to enable this except don't put your code in global scope.