Q:

Why the variable b is not hoisted in this example?

I am reading Javascript Definitive guide and i came across a statement about function scope:"JavaScript’s function scope means that all variables declared within a function are visible throughout the body of the function"
Since bar() has a variable b in it which is in turn declared within function foo().Why b is not hoisted in this case and output is not undefined(Inner variable b should be visible throughout the body of foo() )?
var b =5 ; 
    function foo(x)
    {
      console.log(b);
      function bar()
      {
       var b=4;
      }

    }
    foo(4);

A:

The scope of b within bar is the bar function. Variables are not visible outside their function. That would mean that all variables would be visible globally, since they would all propagate up into the global scope/outermost function. That's precisely what scope is supposed to prevent.
b is hoisted within its scoping function, which is bar; not foo.

