What's the difference between variable definition and declaration in JavaScript?

后端 未结 8 1372
情话喂你
情话喂你 2020-12-03 03:54

Is this a variable definition or declaration? And why?

var x;

..and is the memory reserved for x after this st

8条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 04:12

    var x, y, z;
    
    var x;
    
    var h = 4;
    
    i = 4;
    

    all the above are global variables if placed at the top, (outside any functions)

    Lets say that the javascript has a function start

    function start() {
          x = 5*5;
    }
    

    the global variable x is now equal to 25

    Where as if the var x; was not placed outside of any functions, that variable x would just be local to that function.

提交回复
热议问题