Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 634
Daniel Szabo Avatar asked Nov 20 '11 17:11

Daniel Szabo


People also ask

What are command alert () and console log () use for what is the difference between these commands?

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.

What is a reason for using the console log () function?

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.

What is console alert?

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.

What is the function alert () used for?

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.


2 Answers

  • 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
like image 160
Tomasz Nurkiewicz Avatar answered Nov 10 '22 02:11

Tomasz Nurkiewicz


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) 
like image 25
lonesomeday Avatar answered Nov 10 '22 02:11

lonesomeday