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() converts the object passed to it into a string using the object's toString() method. Unlike alert(), console. log() is not only limited to displaying a simple string but also allow you to interact with the object passed to it. For example letting you inspect its properties.
The console. log() is a function in JavaScript which is used to print any kind of variables defined before in it or to just print any message that needs to be displayed to the user.
Alerts are sent to predetermined queues within the Alert Console and can be assigned to a specific user within that queue. Queues are used to direct specific types of alerts to a specific group of users. Users who belong to a queue use the Alert Console to view and close alerts.
One useful function that's native to JavaScript is the alert() function. This function will display text in a dialog box that pops up on the screen. Before this function can work, we must first call the showAlert() function. JavaScript functions are called in response to events.
alert()
is blockingalert()
cannot be easily suppressed in non-debug environmentconsole
typically formats your objects nicely and allows to traverse themalert()
message at a timeconsole
s can have different logging levels with intuitive formattingTry 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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With