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?
alert()is blockingalert()cannot be easily suppressed in non-debug environmentconsoletypically 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
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)
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.
It is non-blocking and allows you to deeply examine objects (rather then just seeing the results of toString()ing them).
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.
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.
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.
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.
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.
来源:https://stackoverflow.com/questions/8203473/why-is-console-log-considered-better-than-alert