3

Regarding the "console.log" function, is it used only for testing/debugging purposes? So would I have to remove them for the final coding stage where everything is completed? I'm confused. For instance, when I'm declaring a function, how would I change the following?

function greeting() { console.log('Hello World'); }

I apologize if this is a stupid question.

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50
Joseph Kim
  • 69
  • 1
  • 5
  • 2
    It's not a stupid question, but unfortunately it's not a question fit for StackOverflow (I voted to close as *"opinion based"*). It depends on the programmer. For instance, I always remove all `console.log`s, but sometimes I leave some `console.info` and `console.warn`, just to log some important information for other users/programers. I've seen big websites, made by professionals, logging all kinds of stuff, like arrays or objects. It must be said, however, that too much `console.log` will have a negative impact on performance. – Gerardo Furtado Dec 21 '19 at 05:53
  • "too much console.log will have a negative impact on performance". @GerardoFurtado could I get a link to an article/documentation that describes this? p.s. New to JS. – kalpaj agrawalla Dec 21 '19 at 06:20
  • 1
    @kalpajagrawalla you'll find some info here: https://stackoverflow.com/q/11426185/5768908. Actually, this doesn't even need a benchmark, empirical evidence is enough: when you have a `console.log` inside a loop you can clearly see that the code is hundreds or thousands of times slower. – Gerardo Furtado Dec 21 '19 at 08:25

4 Answers4

3

console.log or any console methods are for showing any information on browser developer console

It varies, Production env means it's solely for end user and everything is correct to make your website useful. That is why websites have dev/ QA/ Stage env where you want to have some information for developers.

console.log or any methods associated to console uses developer console of the browser, with that said it's clear that it totally meant for someone who is using your website more then just getting information [hacker, developer, auditor, ] etc.

For End user which we expect to be just the normal people who are on your website for their need and don't have any technical requirement, then loggin an error to console is useless ! Instead you need some kind of pop up or notification to inform end user that something went wrong or any useful information.

But there is kind of useful scenarios which you want to inform user to avoid any bad activity suggested by any hacker. Like facebook does, as many of the hacker uses JS snippets to do some automatic changes to website ask normal users to use console and post their snippets. In this case console methods are useful

enter image description here

Satyam Pathak
  • 6,612
  • 3
  • 25
  • 52
1

console.log is used for outputting/logging text to the console. It is not necessary to remove it when you release the web app/website.

For more information on how to use console.log, click here.

sportzpikachu
  • 831
  • 5
  • 18
1

It is mainly for printing relevant info to the browsers console window, although there is a better alternative for debugging. See debugger statement. I would not leave console.logs lying around all over the place whereas it would seem a bit sloppy. Besides, it could be harder to follow depending on how many there are.

There are however times when you would want to leave the console logs and those times are merely for conditional local or server environment execution.

Jnr
  • 1,504
  • 2
  • 21
  • 36
0

Yes, it should be used only for testing/debugging purposes. I would suggest not to use it even for debugging/testing purposes. Better, start a debugger, attach a breakpoint on a statement that you want to analyze and you are sorted.

Example in https://developers.google.com/web/tools/chrome-devtools/javascript

ThinkGeek
  • 4,749
  • 13
  • 44
  • 91