Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Alert is bad" - really? [closed]

Tags:

There's this idea running around that "alert() is bad".

Acknowledgements:

  • Sure, we rarely want to use it in an actual UI design since there are better ways to communicate with users.
  • For debugging, console.log() has much more value than alert().
  • Certain situations (like use of setTimeout) run into problems when alert() gets in the way.
  • Actual debuggers handle pausing and resuming of execution much better than alert(), if that's what a developer needs.

Questions:

  1. Is there a solid, logical reason to never use alert()?
  2. Does the increased value of console.log() truly reduce the value of alert() so drastically that it goes from "useful in limited scenarios" to "bad"?
  3. What do you say to someone who wants to use alert() in a brief test where logging is not setup and any side effects are irrelevant (think tutorials or quick prototypes)?
like image 892
John Fisher Avatar asked Jan 11 '12 19:01

John Fisher


People also ask

Why is alert bad?

It's considered bad design because it prevents additional browser actions (like the back button) and code execution (additional JavaScript and page rendering) until the user clicks the "ok" button.

Should I use Alert in JS?

Alerts should never be used for debugging unless you intend for it to stop the execution of the code for a purpose. Otherwise, you should be using console. log because alert can actually change the result of your code if your code involves asynchronous logic.


2 Answers

Is there a solid, logical reason to never use alert()?

alert is bad simply because it has no positive features and only negative features

  • blocks the entire browser
  • blocks the javascript thread
  • only prints strings
  • requires user interaction to continue (this means you can't automate browser usage)
  • is blocked by common popup blockers
  • doesn't work in non-browser environments like node.js (however console.log does work in node.js)

Does the increased value of console.log() truly reduce the value of alert() so drastically that it goes from "useful in limited scenarios" to "bad"?

Yes, although there are some exceptions

The only value alert has is as a quick hackish tool to debug legacy browser or as a tool to annoy users.

like image 57
Raynos Avatar answered Sep 28 '22 12:09

Raynos


  1. No, it is just a language feature and there is no reason to never use alert().
  2. alert() works differently than console.log(), also console is not always available, so console.log() may reduce the value of alert(), but surely it can not always be replaced.
  3. Explain how console.log() differs from alert(), especially that alert() must output the string, so it must first convert the value to string - it is very important if you want to check what value you have at some point and you picked alert() to fulfill that task. It also stops the execution of the script (this may be useful sometimes).

More links:

  • documentation of window.alert() on Mozilla Developer Network,
like image 30
zizozu Avatar answered Sep 28 '22 12:09

zizozu