1

I want to know if there are any down sides to leaving console.log in my JavaScript functions as debug.

JavaScript:

var debugMSG = "DEBUG: "
var debugFStart = debugMSG + "Starting Function. Name; "
var debugFFinish = debugMSG + "Ending Function. Name; "

function myFunction1() {
      console.log(debugFStart + "myFunction1()")
      alert("Function 1 is being used!")
      console.log(debugFFinish + "myFunction1()")
}
function myFunction2() {
      console.log(debugFStart + "myFunction2()")
      alert("Function 2 is being used!")
      console.log(debugFFinish + "myFunction2()")
}

Would it be bad in any way to leave this code inside of my Websites Files when i upload it?

Jedd Dryden
  • 23
  • 1
  • 6

4 Answers4

3

console.log is a development tool - it has nothing to do with your app's functionality, and as such it shouldn't be deployed to production.

Remember, the more code you have, the bigger chances of introducing bugs. Therefore you should be as concise with it as possible (but not more, to paraphrase Einstein ;) ) For example in this case you have to remember to account for the browser to not have console object implemented. In that case your console.log construction will cause an error. And yes, you can account for that, conditionally make some mock object, but hey, you've just made your app more complicated.

Tomek Sułkowski
  • 7,081
  • 2
  • 17
  • 15
2

http://caniuse.com/#search=console

IE8/9 : Only supports console functions when developer tools are open, otherwise the console object is undefined and any calls will throw errors.

I would suggest to add a definition to prevent those cases as in this question.

I came across a website where the first script had a console.log in it and all the following scripts were failing because of that.

If you can use the full potential of the console object I would recommend those readings :

Community
  • 1
  • 1
nobe4
  • 2,802
  • 3
  • 28
  • 54
1

Yes, it makes your code a lot less readable. That could be reduced to

function myFunction1() {
    alert("Function 1 is being used!")
}
function myFunction2() {
    alert("Function 2 is being used!")
}

Nice and simple.

If you need logging, use a logger (console.log() being a simple logger).

If you need debugging, use the debugger.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
0

Not all browsers support console.log. Old IE doesn't support console.log. So if you app/website still support those old browsers better remove the console log or you can just create a fallback function to support it.

Testing for console.log statements in IE

Community
  • 1
  • 1
3s2ng
  • 923
  • 1
  • 11
  • 19