On the problem of variable lifting in functions

A title goes like this:

var a=2;
function f() {
  console.log(a);
  var a=3;
}  
f();// undefined
f(4);// undefined

Why are f () and f (4) undefined? Isn"t this variable promotion

Feb.28,2021

  • f () : yes, it has been promoted, so there is an in the f function when console.log () is executed, and it will not be found in the external scope. When console.log () is executed, it is only declared and not assigned, so it is undefined
  • .
  • f (4) : your function has no formal parameters defined at all, and there is no internal use of arguments variables, so passing everything is the same as not passing anything, which is equivalent to f ()
  • .
< hr >
  1. arguments will receive all the parameters you pass in, regardless of whether or not you have defined them or how many.

    
    

    f();

        var a=2;
        function f() {
          var a; // 
          console.log(a);
          var a=3;
        }

    f (4):
    your f () has no defined parameters, which is the same as f ()

Menu