0

console.log(localVariable keeps giving a reference error

var globalVariable = "A"; // a global variable
        function testScope1() {
        var localVariable = "B"; // a local variable
        
        
        }

        console.log(globalVariable); // A
        console.log(localVariable); // ReferenceError
  • The local variable is not visible outside the function it's declared in. – VLAZ Apr 16 '22 at 14:13
  • okay, so how do i print it out then....the code has to be in the function? – Micheal Apr 16 '22 at 14:19
  • Either return it or print it from inside the function. In any case, you have to actually run the function to even get the variable declared. – VLAZ Apr 16 '22 at 14:24

0 Answers0