0

Had a little doubt in one case. If I have a variable declared in global scope, I know it gets attached to window object.

var var1 = 'Apple';

If I have a variable declared in a function, that variable is in functional scope.

function testFunc() {
   var var2 = 'zebra';
}

But if I invoke a function and pass a parameter, I know an assignment operation happens.

eg 1) function testFunct(param1) {
        console.log('which scope is param1');
      }
      testFunct('Mango');

I know assignment operation var param1 = 'Mango'; happens, but where? at invoking place or when invoked? And what is the scope of param1, global or functional.

And if a global variable was declared and passed in invocation,

eg 2)  function someFucntionCall(param) {
         console.log('what is the scope of param here');
       }
       var someVar = 'Merc';
       someFucntionCall(someVar);  //global variable declared and passed in function invocation

What is the scope of param here? Bit confused in these, couldn't find proper explanation anywhere.

Uzair Khan
  • 2,812
  • 7
  • 30
  • 48
  • 1
    The scope of a parameter variable is the function body, just like a variable declared in the main block of the function. – Barmar Feb 04 '20 at 10:27
  • But if it was declared and initialised in global scope and passed in function invocation, then? My example 2 – Uzair Khan Feb 04 '20 at 10:29
  • 1
    The variable you passed in has nothing to do with the local variable in the function. Just the value is passed. – Barmar Feb 04 '20 at 10:39
  • Okay, no matter how passed, the parameter of a function is always local to it i.e in functional scope only. Thanks Barmar – Uzair Khan Feb 04 '20 at 10:48

0 Answers0