Hoisting variables in JavaScript

后端 未结 5 1515
误落风尘
误落风尘 2020-12-06 20:50

I understand Hoisting of variables is done in Java Script. I am unable to get why it outputs as undefined

 do_something()
    {
    var foo = 2;    
    cons         


        
5条回答
  •  無奈伤痛
    2020-12-06 21:54

    The second function outputs undefined because you set the variable after you called it. The code you wrote is synchronous which means its read by the interpreter line by line, executed in order. If you don't define the variable before calling it, the console outputs undefined.

    This article explains it in more detail.

提交回复
热议问题