running function created with two ways in javascript -


var = function(){      alert("2");  }    function a(){      alert("1");  }    a();

as above, declared 2 functions in different ways. when run a(), got 2. why?

when declare function or variable in javascript gets "hoisted", meaning javascript interpreter pretends variable (a in case) declared @ top of file scope.

when declaration takes form function a() ..., definition of function hoisted along declaration.

assignments a = function ()... don't hoisted, , assignment happens after function a() ... piece , overrides it.


Comments