There's this idea running around that "alert() is bad".
Acknowledgements:
console.log()
has much more value than alert()
.setTimeout
) run into problems when alert()
gets in the way.alert()
, if that's what a developer needs.Questions:
alert()
?console.log()
truly reduce the value of alert()
so drastically that it goes from "useful in limited scenarios" to "bad"?alert()
in a brief test where logging is not setup and any side effects are irrelevant (think tutorials or quick prototypes)?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.
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.
Is there a solid, logical reason to never use alert()?
alert is bad simply because it has no positive features and only negative features
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.
alert()
.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.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:
window.alert()
on Mozilla Developer Network,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