Reputation: 1289
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";
I clear and close the console, reopen it, and look for the value of name
, it's still Bob
.
What's the best way to clear these out?
Upvotes: 80
Views: 65636
Reputation: 1
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.
Upvotes: -1
Reputation: 9053
name
is already defined.
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.
(this was really confusing me - but would not have - if I had chosen almost any other variable for my class demonstration!)
Upvotes: 0
Reputation: 1
It seems the console history can be cleared through right mouse button context menu.
Upvotes: -1
Reputation: 27347
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
.
Upvotes: 53
Reputation: 1390
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.
Upvotes: 15
Reputation: 21
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.
Upvotes: 2
Reputation: 530
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.
Upvotes: 2
Reputation: 197
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.
Upvotes: 1
Reputation: 159
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,
Upvotes: -7
Reputation: 1939
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
Upvotes: 59
Reputation: 2148
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!
Upvotes: 22