0

I was reading the Scopes and Closure of You don't know JS book by Kyle Simpson, specifically this topic Error.

function foo(a) {
  console.log( a + b );
  b = a;
} 
foo( 2 );

This example results in reference error. In the book, it said that "b" is not found in the scope. Can anyone help me realize why "b" is not found in that scope?

Craicerjack
  • 6,203
  • 2
  • 31
  • 39
  • 1
    Possible duplicate of [ReferenceError: variable is not defined](https://stackoverflow.com/questions/17583544/referenceerror-variable-is-not-defined) – 31piy May 08 '18 at 11:09
  • Possible duplicate of [javascript hoisting for global variable](https://stackoverflow.com/questions/26753166/javascript-hoisting-for-global-variable) – Craicerjack May 08 '18 at 11:18

1 Answers1

0

Assignments are not hoisted in JavaScript so when you run
console.log( a + b );
b has not been defined and so is not found in the scope.

Craicerjack
  • 6,203
  • 2
  • 31
  • 39