In Chrome and Firefox,
typeof foo
evalulates to \'undefined\'.
But
typeof (function() { return foo; })
The error "ReferenceError: foo is not defined" is not being thrown by typeof, its being thrown by the function itself. If you had used:
typeof (function() { return 2; })()
it would have returned "number" as expected, but in this example JavaScript is not even getting to the point where typeof is being run on anything. You are receiving the same error as if you had run:
function test () {
return foo;
}
test();