Why is console.log() considered better than alert()?

Daniel Szabo picture Daniel Szabo · Nov 20, 2011 · Viewed 23.3k times · Source

I've always been told that when debugging an application, JavaScript's console.log() method is preferred over simply using an alert() method. Why is this? Is there a good example someone can point me to where console.log() is obviously the better choice?

Answer

Tomasz Nurkiewicz picture Tomasz Nurkiewicz · Nov 20, 2011
  • alert() is blocking
  • alert() cannot be easily suppressed in non-debug environment
  • console typically formats your objects nicely and allows to traverse them
  • logging statements often have an interactive pointer to code which issued logging statement
  • you cannot look at more than one alert() message at a time
  • consoles can have different logging levels with intuitive formatting