Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad idea to leave "console.log()" calls in your production JavaScript code?

I have a bunch of console.log() calls in my JavaScript.

Should I comment them out before I deploy to production?

I'd like to just leave them there, so I don't have to go to the trouble of re-adding the comments later on if I need to do any more debugging. Is this a bad idea?

like image 685
Charlie Kotter Avatar asked Jul 11 '09 17:07

Charlie Kotter


People also ask

Should console log be in production code?

Generally yes, its not a great idea to expose log messages in your production code. Ideally, you should remove such log messages with a build script before deployment; but many (most) people do not use a build process (including me).

Is console log bad for performance?

Yes, it will reduce the speed, though only negligibly. But, don't use it as it's too easy for a person to read your logs.

What is the purpose of using console log in JavaScript?

The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.


2 Answers

It will cause Javascript errors, terminating the execution of the block of Javascript containing the error.

You could, however, define a dummy function that's a no-op when Firebug is not active:

if(typeof console === "undefined") {     console = { log: function() { } }; } 

If you use any methods other than log, you would need to stub out those as well.

like image 58
Jason Creighton Avatar answered Oct 13 '22 04:10

Jason Creighton


As others have already pointed it, leaving it in will cause errors in some browsers, but those errors can be worked around by putting in some stubs.

However, I would not only comment them out, but outright remove those lines. It just seems sloppy to do otherwise. Perhaps I'm being pedantic, but I don't think that "production" code should include "debug" code at all, even in commented form. If you leave comments in at all, those comments should describe what the code is doing, or the reasoning behind it--not blocks of disabled code. (Although, most comments should be removed automatically by your minification process. You are minimizing, right?)

Also, in several years of working with JavaScript, I can't recall ever coming back to a function and saying "Gee, I wish I'd left those console.logs in place here!" In general, when I am "done" with working on a function, and later have to come back to it, I'm coming back to fix some other problem. Whatever that new problem is, if the console.logs from a previous round of work could have been helpful, then I'd have spotted the problem the first time. In other words, if I come back to something, I'm not likely to need exactly the same debug information as I needed on previous occasions.

Just my two cents... Good luck!

like image 40
Chris Nielsen Avatar answered Oct 13 '22 04:10

Chris Nielsen