Chrome console clear assignment and variables

Just Reload for new context with clear history and old commands execution

Things have changed a lot on Chrome dev tools. delete name does not help;

//for example if you have declared a variable 
const x = 2;
//Now sometime later you want to use the same variable 
const x = 5; // you will get an error 
//if you try to delete it you will get false
delete x;

But a reload of page while console is still open. will refresh the console context and now the x is not available and you can redefine it.


A simple solution to this problem is to wrap any code that you don't want in the global scope in an immediately-invoked function expression (IIFE). All the variables assigned in the function's scope are deallocated when the function ends:

(function() {

    // Put your code here...

})();

For more info on IIFEs: https://en.wikipedia.org/wiki/Immediately-invoked_function_expression

[Update]

In ES6 you can use blocks (so long as you use let instead of var):

{

    // Put your code here...

}

For more info on blocks: http://exploringjs.com/es6/ch_core-features.html#sec_from-iifes-to-blocks


Easiest way to clear data from the console is to refresh the page.

What you are affecting when declaring any variables or functions within the developer console is the global execution context, which for web browsers is window.

When you clear() the console you are telling Chrome to remove all visible history of these operations, not clear the objects that you have attached to window.


Clearing the console doesn't clear out the variables from the memory rather it just clears the data from the user interface.

Reloading the page clears the console data. I hope that should be fine as you mentioned that you are just testing and learning javascript.

Hope that helps!