How to unset a JavaScript variable?

后端 未结 11 2033
执笔经年
执笔经年 2020-11-22 04:16

I have a global variable in JavaScript (actually a window property, but I don\'t think it matters) which was already populated by a previous script but I don\'t

11条回答
  •  广开言路
    2020-11-22 04:47

    In addition to what everyone had written, also note that delete returns boolean. It can tell you if the delete was successful or not.

    Testing on Chrome, everything except let was deletable. when delete returned true it actually removed them:

    implicit_global = 1;
    window.explicit_global = 1;
    function_set = function() {};
    function function_dec() { };
    var declared_variable = 1;
    let let_variable = 1;
    
    delete implicit_global; // true, tested on Chrome 52
    delete window.explicit_global; // true, tested on Chrome 52
    delete function_set; // true, tested on Chrome 52
    delete function_dec; // true, tested on Chrome 52
    delete declared_variable; // true, tested on Chrome 52
    delete let_variable; // false, tested on Chrome 78
    

提交回复
热议问题