24

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?

Michał Perłakowski
  • 88,409
  • 26
  • 156
  • 177
Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65

9 Answers9

42
  • 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
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 1
    don't forget if(window.console)console.log or you will break crap for IE users since the console don't exist until it is open. – FlavorScape Jun 26 '12 at 19:53
  • 2
    I'm not sure what you mean by saying alert can't be easily supressed. All you need is `alert = function(){}` – Peter Olson Jun 26 '12 at 20:01
  • 1
    @PeterOlson -- alert is not just old school , it is hideous and invasive .. Sure you can cleverly work around it , but not the greatest thing to do , why not have every developer be consistent and using F12 developer tools and stop with nasty alerts. – Tom Stickel Apr 09 '16 at 06:53
  • 1
    IE already breaks, no `console.log()` needed. This is only on older versions of IE, and if you are supporting that, you are wasting your precious time. Go make something awesome, not support ancient browsers. –  Feb 03 '17 at 17:53
  • But many times there is a definite difference between the results shown by console.log and alert. I was trying a program where a key assignment in an object is ignored if the key is user-provided & it's value is "__proto__". The console shows an empty object as output i.e. {}, while alert shows "[Object object]". It seems like @lonesomeday has also mentioned the same problem below. – Chinmay Ghule Jun 24 '21 at 13:30
9

Try this:

var data = {a: 'foo', b: 'bar'};
console.log(data);
alert(data);

You'll see that console.log shows you the object, while alert gives you [object Object], which isn't useful. This is true also of, e.g. elements:

alert(document.body); // [object HTMLBodyElement] (exact result depends on your browser)
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
5

Both are just a way to get information about what's happening at the time in your JS. I used to use alert() all the time but migrated to console.log() for a few reasons. (Side note: console offers more than just log(), take a look at what else it can do).

I think the main benefits of console.log() are:

  • it does not halt processes like alert does
  • you can see what line of what script threw the log entry without putting the line into your message
  • if you have more than one thing you're debugging it can get real annoying to keep hitting 'ok' on your alert boxes
  • you can log objects and get lots of good info (thanks for the reminder, other answerers)

In the end it boils down to how you prefer to debug.

One thing to be aware of. Not all browsers SUPPORT console.log() and will have a problem if you leave your console.log() calls in your code. Provide a console stub if console is not available to get around that problem.

greenanvil
  • 238
  • 1
  • 2
  • 7
3

It is non-blocking and allows you to deeply examine objects (rather then just seeing the results of toString()ing them).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

The alert needs to be dismissed before javascript execution can resume. console.log has no such problem.

console.log will also display the object with values, where a call to alert will require you to traverse the object first.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
2

I guess its somewhat a matter of taste, but there are a number of benefits of using console.log:

  • Say you want to log 20 different things, that would be quite annoying with alert.
  • You can log objects for instance, and then inspect them.
  • In Chrome Dev tools for instance, you can preserve the log between different pages.
  • It is non-blocking
  • It does not affect the end user if forgotten

To name a few.

Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
1

If you forget to remove a debugging alert statement it will directly affect the end user.
If you forget to remove a debuggins console.log statement the user is not affected.
In addition console.log will allow you to see the full contents of an object instead of JavaScript's toString() representation.

John Hartsock
  • 85,422
  • 23
  • 131
  • 146
0

Because alerts are a PITA, stop everything until there's input, and don't allow object introspection.

Using a debugger is even better under some circumstances.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

alert() stops all interaction with the browser until the message is dismissed while console.log() just prints the message to the console.

Ex. You're printing the values of a bunch of variables to make sure they're right and don't want to dismiss the alert window after each variable.

Firenze
  • 308
  • 1
  • 9