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
93.4k30 gold badges165 silver badges189 bronze badges
asked Nov 20, 2011 at 17:55

9 Answers 9

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
answered Nov 20, 2011 at 17:58
Sign up to request clarification or add additional context in comments.

5 Comments

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.
I'm not sure what you mean by saying alert can't be easily supressed. All you need is alert = function(){}
@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.
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.
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.
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)
answered Nov 20, 2011 at 17:58

Comments

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.

answered Nov 20, 2011 at 18:02

Comments

3

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

answered Nov 20, 2011 at 17:57

Comments

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.

answered Nov 20, 2011 at 17:57

Comments

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.

answered Nov 20, 2011 at 17:59

Comments

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.

answered Nov 20, 2011 at 17:58

Comments

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.

answered Nov 20, 2011 at 17:58

Comments

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.

answered Nov 20, 2011 at 18:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.