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

后端 未结 8 1373
情话喂你
情话喂你 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:27

    Declaring a variable is like telling the (javascript) compiler that this token x is something I want to use later. It does point to a location in memory, but it does not yet contain a value. ie. it is undefined

    var x;
    

    defining it means to give it a value which you can either do it like:

    x = 10; // defining a variable that was declared previously
    

    or like this:

    var y = 20; // declaring and defining a variable altogether.
    

    http://msdn.microsoft.com/en-us/library/67defydd(v=vs.94).aspx http://www.w3schools.com/js/js_variables.asp

提交回复
热议问题