Zachary Burt Zachary Burt - 1 year ago 53
Javascript Question

How to quickly and conveniently disable all console.log statements in my code?

Is there any way to turn off all

statements in my JavaScript code, for testing purposes?

Answer Source

Redefine the console.log function in your script.

console.log = function() {}

That's it, no more messages to console.


Expanding on Cide's idea. A custom logger which you can use to toggle logging on/off from your code.

Working Demo →

From my Firefox console:

var logger = function()
    var oldConsoleLog = null;
    var pub = {};

    pub.enableLogger =  function enableLogger() 
                            if(oldConsoleLog == null)

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

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

    return pub;


        console.log('hi', 'hiya');
        console.log('this wont show up in console');

        console.log('This will show up!');

How to use the above 'logger'? In your ready event, call logger.disableLogger so that console messages are not logged. Add calls to logger.enableLogger and logger.disableLogger inside the method for which you want to log messages to the console.