229

I was wondering if I could clear up the console with some command..

console.log(), can print... is there a command to clear up console?..

I've tried to console.log(console); and got this functions below...

assert: function assert() { [native code] }
constructor: function Console() { [native code] }
count: function count() { [native code] }
debug: function debug() { [native code] }
dir: function dir() { [native code] }
dirxml: function dirxml() { [native code] }
error: function error() { [native code] }
group: function group() { [native code] }
groupEnd: function groupEnd() { [native code] }
info: function info() { [native code] }
log: function log() { [native code] }
markTimeline: function markTimeline() { [native code] }
profile: function profile() { [native code] }
profileEnd: function profileEnd() { [native code] }
time: function time() { [native code] }
timeEnd: function timeEnd() { [native code] }
trace: function trace() { [native code] }
warn: function warn() { [native code] }
__proto__: Object

[ I guess there's no way to clear up the console... but I wanted someone to say it to me... ]

Samuel Neff
  • 73,278
  • 17
  • 138
  • 182
Reigel Gallarde
  • 64,198
  • 21
  • 121
  • 139
  • Related posts - [How to clear Chrome console by shortcut keys?](https://stackoverflow.com/q/5483451/465053) & [How to remove all recent console command](https://stackoverflow.com/q/21149156/465053) – RBT Jun 21 '19 at 02:14

19 Answers19

329

Update: console.clear() is available in all browsers

Update: As of November 6, 2012, console.clear() is now available in Chrome Canary.


If you type clear() into the console it clears it.

I don't think there is a way to programmatically do it, as it could be misused. (console is cleared by some web page, end user can't access error information)

one possible workaround:

in the console type window.clear = clear, then you'll be able to use clear in any script on your page.

mh-anwar
  • 131
  • 3
  • 10
cobbal
  • 69,903
  • 20
  • 143
  • 156
  • 5
    duh... the javascript function is `clear()` ... it's the same thing. – chakrit Jun 10 '10 at 04:22
  • @cobbal good point... but I don't get the 'it could be misused' part... can you share some knowledge about it? – Reigel Gallarde Jun 10 '10 at 04:25
  • 1
    A possible scenario where it would be annoying: some person develops a webpage, using the clear command and it makes its way into production code. You navigate to the page and something breaks. You can't figure out why since the console has been cleared by the page. – cobbal Jun 10 '10 at 04:28
  • @chakrit try opening the url `javascript:alert(clear)`. it will say "clear is not defined" – cobbal Jun 10 '10 at 04:29
  • @cobbal Indeed, my mistake. What I did binded a closure with `clear` from the console, so it works. Executing it outside of console doesn't. – chakrit Jun 10 '10 at 04:33
  • productivity hint: click the 'clear' button in the status bar. same effect, saves you 7 keystrokes. – schellmax Nov 23 '11 at 08:19
  • 1
    @schellmax Yeah, but you have to use the mouse. No fun. – Ken Bellows Jul 18 '12 at 20:28
  • 41
    console.clear() is now available in Chrome. Try it in Canary. – Paul Irish Nov 07 '12 at 20:03
  • 25
    You can simply hit CTRL+L while cursor is focused in the console. All console output will be wiped. – Lido Feb 05 '14 at 10:35
  • Well, I was able to access clear problematically, this must have changed. To be honest, I think it should be accessible, I mean, what if you have a Single Page app, that allows people to opt-in to logging. When someone logs off you don't really want the next person to come a long and look at all the logging. console.clear() on log-off completion is a good fix to this problem. – Josh Mc Apr 11 '14 at 00:23
  • 19
    Just a note, `clear()` doesn't work if you have checked "Preserve log upon navigation". – beatgammit Apr 24 '14 at 21:40
  • console object in [Chrome](https://developer.chrome.com/devtools/docs/console-api#consoleclear) browser. `if (console) { console.log('console command/object is defined ') }` If IE Browser throws an error console as undefined Object then make changes F12 ► [Browser mode menu](https://msdn.microsoft.com/en-us/library/gg589500(v=vs.85).aspx) ► choose the Compatibility View option. To Open [BrowserConsel](http://webmasters.stackexchange.com/questions/8525/how-to-open-the-javascript-console-in-different-browsers). – Yash Aug 11 '15 at 11:29
  • I get the message `console.clear() was prevented due to 'Preserve log'` –  Mar 19 '19 at 08:41
135

There's always the good ol' trick:

console.log("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");

or a shorter variation of the above:

console.log('\n'.repeat('25'));

Not the most elegant solution, I know :) ... but works.

For me, I usually just print a long "-----" separator line to help make the logs easier to read.

Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
chakrit
  • 61,017
  • 25
  • 133
  • 162
  • 115
    When you can't use a scalpel, use an axe. +1 – mVChr Jun 10 '10 at 04:54
  • 43
    `console.log(new Array(24 + 1).join('\n')` does the same thing, but it's much shorter. :) – alexia Feb 02 '11 at 15:08
  • 18
    @Nyuszika7H If you're after conciseness: `console.log(Array(25).join('\n'))`. Calling the `Array` constructor without the `new` operator is spec'd to return an array. – alex Apr 16 '12 at 23:49
  • 2
    Nice suggestions. I'm preparing a web presentation with console.info as "presenter notes" (following [Christian's advice](http://christianheilmann.com/2012/08/15/browsers-have-a-presenter-mode-console-info/)), and this is what I came up with, just in case I won't remember to assign `window.alert`: `var hasClear = (typeof clear == 'function'); if(hasClear) clear(); console.info('bla'); if(!hasClear) console.log(Array(18).join('\n'));` – Ronny Sep 06 '12 at 14:15
  • 1
    I would opt for the hard coded string, as it is more readable code. You can look at it for 1 second and know exactly what it does. And I guess there is a performance gain too. – Kevin Wheeler Jun 06 '15 at 17:53
  • This answer has the most ghetto:upvotes ratio I've ever seen on SO – Chunky Chunk Jul 19 '16 at 00:00
  • @chakrit You made my day!) – voodoo417 Sep 19 '16 at 11:50
  • @chakrit Cool. But I get **undefined** above the blinking cursor :-D – Bharath theorare Jan 03 '17 at 12:54
  • 1
    `console.log('\n'.repeat(25))` without Array.join :) – dKorosec Jan 11 '17 at 22:06
44

This seems to work just fine:

console.clear();
RealWorldCoder
  • 1,031
  • 1
  • 10
  • 16
23

If you use console.clear(), that seems to work in chrome. Note, it will output a "Console was cleared" message.

I tested this by racking up a ton of Javascript errors.

Note, I got an error right after clearing the console, so it doesn't disable the console, only clears it. Also, I have only tried this in chrome, so I dont know how cross-browser it is.

EDIT: I tested this in Chrome, IE, Firefox, and Opera. It works in Chrome, MSIE and Opera's default consoles, but not in Firefox's, however, it does work in Firebug.

Ben
  • 2,200
  • 20
  • 30
20

Chrome Console Clear button

If you want to just clear the console when debugging, you can simply click the "ban-circle" button to clear console.log.

Alternatively just press "Ctrl+L" to clear the console using your keyboard.

Dean Meehan
  • 2,511
  • 22
  • 36
14

Chrome:

console._commandLineAPI.clear();

Safari:

console._inspectorCommandLineAPI.clear();

You can create your own variable, which works in both:

if (typeof console._commandLineAPI !== 'undefined') {
    console.API = console._commandLineAPI;
} else if (typeof console._inspectorCommandLineAPI !== 'undefined') {
    console.API = console._inspectorCommandLineAPI;
} else if (typeof console.clear !== 'undefined') {
    console.API = console;
}

After that, you can simply use console.API.clear().

alexia
  • 14,440
  • 8
  • 42
  • 52
  • 5
    This only works through the console itself on Chrome, in which case you can also just do `clear()` out of the box. – pimvdb Aug 12 '12 at 09:24
13

you can use

console.clear();

if you are working with javascript coding.

else you can use CTR+L to clear cosole editor.

Avinash Raut
  • 1,872
  • 20
  • 26
9

Press CTRL+L Shortcut to clear log, even if you have ticked Preserve log option.
Hope this helps.

AbhiNickz
  • 1,035
  • 2
  • 14
  • 32
  • 1
    Used `clear()` for such a long time, yet I never thought to use Bash's notorious clear-terminal shortcut. :) Thanks! – iamdanchiv Jul 09 '17 at 13:38
8

On the Mac you can also use ⌘+K just like in Terminal.

6

Instead of typing command just press:

CLTRL + L

to clear chrome console

Ilyas karim
  • 4,592
  • 4
  • 33
  • 47
5
console._inspectorCommandLineAPI.clear()

That is working

E-D
  • 115
  • 2
  • 8
5

A handy compilation of multiple answers for clearing the console programmatically (from a script, not the console itself):

if(console._commandLineAPI && console._commandLineAPI.clear){
    console._commandLineAPI.clear();//clear in some safari versions
}else if(console._inspectorCommandLineAPI && console._inspectorCommandLineAPI.clear){
    console._inspectorCommandLineAPI.clear();//clear in some chrome versions
}else if(console.clear){
    console.clear();//clear in other chrome versions (maybe more browsers?)
}else{
    console.log(Array(100).join("\n"));//print 100 newlines if nothing else works
}
4

I use the following to alias cls when debugging locally in Chrome (enter the following JavaScript into the console):

Object.defineProperty(window, 'cls', {
    get: function () {
        return console.clear();
    }
});

now entering cls in the console will clear the console.

cookch10msu
  • 930
  • 7
  • 10
4

Chrome - Press CTRL + L while focusing the console input.

Firefox - clear() in console input.

Internet Explorer - Press CTRL + L while focusing the console input.

Edge - Press CTRL + L while focusing the console input.

Have a good day!

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
WingmanImd
  • 142
  • 6
4

On the Chrome console right click with the mouse and We have the option to clear the console

Bhanu Kalyan
  • 101
  • 1
  • 5
3

On MacOS:

  1. Chrome - CMD+K
  2. Safari - CMD+K
  3. Firefox - No shortcut

On Linux:

  1. Chrome - CTRL+L
  2. Firefox - No shortcut

On Windows:

  1. Chrome - CTRL+L
  2. IE - CTRL+L
  3. Edge - CTRL+L
  4. Firefox - No shortcut

To make it work in Firefox, userscripts can be used. Download GreaseMonkey extension for FF.

document.addEventListener("keydown",function(event){
    if(event.metaKey && event.which==75) //CMD+K
    {
        console.clear();
    }
});

In the script, update the metadata with the value, //@include *://*/*, to make it run on every pages. It will work only when the focus is on the page. It's just a workaround.

Vignesh Raja
  • 7,927
  • 1
  • 33
  • 42
3

Based on Cobbal's answer, here's what I did:

In my JavaScript I put the following:

setInterval(function() {
  if(window.clear) {
    window.clear();
    console.log("this is highly repeated code");
  }
}, 10);

The conditional code won't run until you ASSIGN window.clear (meaning your log is empty until you do so). IN THE DEBUG CONSOLE TYPE:

window.clear = clear;

Violà - a log that clears itself.

Mac OS 10.6.8 - Chrome 15.0.874.106

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284
1

I think this is no longer available due to 'security issues'.

console.log(console) from code gives:

Console
memory: MemoryInfo
profiles: Array[0]
__proto__: Console

From outside of code, _commandLineAPI is available. Kind of annoying because sometimes I want to just log and not see the old output.

Tatsh
  • 2,780
  • 1
  • 20
  • 23
  • 1
    Don't forget to follow the prototype chain when determining what methods are available on an object :) – alex Apr 16 '12 at 23:51
-1

I'm going to add to this, as it's a search that came out top on google..

When using ExtJS / Javascript I insert this and the console is cleared out - unless there is an error..

console.log('\033[2J');

I'm more than likely way off course, but this is how I clear the console for each page load/refresh.

Cookra
  • 78
  • 8