Chrome console clear assignment and variables

JavascriptGoogle Chrome-Devtools

Javascript Problem Overview


I am learning JavaScript and have been doing a lot of testing in the Chrome console. Even if I clear the console, or use any of the commands I've seen in other threads (localStorage.clear()) any variables I've assigned still show up.

For example, if I do something like var name = "Bob";

enter image description here

I clear and close the console, reopen it, and look for the value of name, it's still Bob.

enter image description here

What's the best way to clear these out?

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

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.

Solution 3 - Javascript

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!

Solution 4 - Javascript

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.

Solution 5 - Javascript

I think the best way to clear the defined varible would be:

window.location.reload();

It would automatically remove all the declared variable at once.

Solution 6 - Javascript

If you right-click the refresh icon in chrome(chrome devtools need to be opened, F12/Right Click on the page -> Inspect), after a second or two you will get the option to hard reload. Hard Reload will clear all stored variables.

Ctrl+Shift+R (Windows) / Cmd+Shift+R (Mac) will do the same without opening the devtools.

Solution 7 - Javascript

If you want to remove that variable then

delete name;

Solution 8 - Javascript

There is actually an answer to this, not sure if this is new or has been here for a while but if you perform a hard refresh i.e. control + shift + r then the console values are completely removed and you can re-instantiate the same variables and functions, etc.

Solution 9 - Javascript

name is already defined.

enter image description here

So - it's already in the global space.

If you do the same thing with a variable like hello - and then refresh, it will clear the memory.

Because name is already defined, it will remain. Refreshing - will not work.

enter image description here

enter image description here

(this was really confusing me - but would not have - if I had chosen almost any other variable for my class demonstration!)

Solution 10 - Javascript

Up until a few days ago CTRL + Shift + R worked for me.

However they have made it very easy to clear console.

Open chrome dev tools and right-click on an empty space. If you right-click on the line with the cursor, it won't show you the right options. The right options are clear console and clear console history.

Clearing console history will clear all variables and their data stored in the console. Hope this helps. It was driving me nuts too and there was no clear answer.

Solution 11 - Javascript

It seems the console history can be cleared through right mouse button context menu.

Solution 12 - Javascript

Console.clear()

Use this command in your browser console and see the magic, very handy to use and works perfectly as per best practices of using chrome console,

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionspex5View Question on Stackoverflow
Solution 1 - JavascriptpjiversView Answer on Stackoverflow
Solution 2 - JavascriptsdgluckView Answer on Stackoverflow
Solution 3 - JavascripthkaseraView Answer on Stackoverflow
Solution 4 - JavascriptTarandeep SinghView Answer on Stackoverflow
Solution 5 - JavascriptPawan lakheraView Answer on Stackoverflow
Solution 6 - JavascriptPmmgView Answer on Stackoverflow
Solution 7 - Javascriptuser2879041View Answer on Stackoverflow
Solution 8 - JavascriptRamiView Answer on Stackoverflow
Solution 9 - JavascriptsheriffderekView Answer on Stackoverflow
Solution 10 - JavascriptIsatu KView Answer on Stackoverflow
Solution 11 - JavascriptAnonView Answer on Stackoverflow
Solution 12 - Javascriptuser2841183View Answer on Stackoverflow