Hide πŸ™ˆ all console logs in production with just 3 lines of code

Kushal sharma - Jun 7 '20 - - Dev Community

We basically use the console.log() in our JS application to check wether our code is working properly or to replicate the bug or issue in the app. without the logs it is very time consuming and difficult to find the problem.

But these logs are meant for the developer only and you don't want to show these to the end users so we have to remove the console statement or to comment that.

Alt Text

Before i know this stuff i was commenting all the console.logs in my application and deploying the app on the live server. so the console's are not visible to the users

How I hide all the consoles

if (env === 'production') {
    console.log = function () {};
}

Here we are overriding the default console.log function with the new one which was returing nothing. Here we have added the environment check to override console function only if the environment is production. if you don't have environment variable then you can jsut simply do.

    console.log = function () {};

I am using this on my live app to hides the console. If someone know any other method or any disadvantage of using this one. plz leave your comment.

If you are on Twitter, can you follow me there as well? I post these kinds of stuff there as well. => Follow @kushal_js

. . . . . . . . . . . . . . .
Terabox Video Player