delete operator with var and non var variables

后端 未结 4 1508
感情败类
感情败类 2020-12-03 19:42

I tried delete a variable in javascript using delete operator, but found some issue. Can you guys please explain the below code, and why it is happening

>         


        
4条回答
  •  无人及你
    2020-12-03 20:19

    The delete operator removes a property from an object not the normal variables. Here in this case the object is global implicitly.

    On a side note:-

    When you write var x = 5 then it declares variable x in current scope ie, in execution context. If declaration appears in a function then local variable is declared and if it's in global scope then global variable is declared.

    Whereas when you say x = 5,then it is merely a property assignment. It first tries to resolve x against scope chain. If it finds x anywhere in that scope chain then it performs assignment otherwise it creates x property on a global object.

提交回复
热议问题