Abhi
Abhi Author of Anicode.in. Passionate full-stack web developer.

Remove all console messages in just one line!


Remove all console messages in just one line!

Ever wonder how to hide all the console messages in any text renderer in a very short span of time that’s too quickly?

Then here is a PRO TIP!

Welcome to abhi codes blog.

Just use this one line of code!

1
2
console.log = function() {};

It will hide the console to open!

This command converts console function into an empty function resulting in “” empty string!

More advanced way

You can create a function to hide the console at your convenience works even in IE<7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var logger = function()
{
    var oldConsoleLog = null;
    var pub = {};

    pub.enableLogger =  function enableLogger() 
                        {
                            if(oldConsoleLog == null)
                                return;

                            window['console']['log'] = oldConsoleLog;
                        };

    pub.disableLogger = function disableLogger()
                        {
                            oldConsoleLog = console.log;
                            window['console']['log'] = function() {};
                        };

    return pub;
}();

You can call the function as enableLogger() or disableLogger() as per your project convenience!

Hope this small tip helps you!

Happy coding!

comments powered by Disqus