Are variables statically or dynamically “scoped” in javascript?

前端 未结 7 1912
無奈伤痛
無奈伤痛 2020-12-13 18:38

Or more specific to what I need:

If I call a function from within another function, is it going to pull the variable from within the calling function, or from the le

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-13 19:15

    As far as I understand, any variable without the var keyword is treated global, with it, its local scoped, so:

    // This is a local scoped variable.
    var local_var = "something"; 
    
    // This is a global scoped variable.
    global_var = "something_else";
    

    As a good JS practice, it is recommended to ALWAYS add the var keyword.

提交回复
热议问题